You must Sign In to post a response.
  • Category: .NET

    How to insert values into database

    hi all
    i have written code to insert values in to database so for to insert image into database i am using file upload control so all is working fine if i insert textbox values and image with fileupload control
    so what the problem is if i want to insert only textbox values but not image with file upload control its showing error so below is my code any body helpme

    FileUpload img = (FileUpload)FileUpload1;
    Byte[] imgByte = null;
    if (FileUpload1.HasFile)
    {

    //To create a PostedFile
    HttpPostedFile File = FileUpload1.PostedFile;
    //Create byte Array with file len
    imgByte = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgByte, 0, File.ContentLength);
    }

    //if (img.HasFile && img.PostedFile != null)
    //{
    // //To create a PostedFile
    // HttpPostedFile File = FileUpload1.PostedFile;
    // //Create byte Array with file len
    // imgByte = new Byte[File.ContentLength];
    // //force the control to load data in array
    // File.InputStream.Read(imgByte, 0, File.ContentLength);
    //}

    // Insert the employee name and image into db
    string conn = "Data Source=HOME;Initial Catalog=NoticeBoard;Integrated Security=True";
    SqlConnection con = new SqlConnection(conn);
    SqlCommand cmd = new SqlCommand("INSERT INTO ekthaemployee(eid,ename,eaddress,epanno,epassword,ecpassword,mobileno,email,pc) VALUES(@eid, @ename,@eaddress,@epanno,@epassword,@ecpassword,@mobileno,@email,@pc) SELECT @@IDENTITY",con);


    //string sql = "INSERT INTO ekthaemployee(eid,ename,eaddress,epanno,epassword,ecpassword,mobileno,email,pc) VALUES(@eid, @ename,@eaddress,@epanno,@epassword,@ecpassword,@mobileno,@email,@pc) SELECT @@IDENTITY";
    //SqlCommand cmd = new SqlCommand(sql, connection);
    cmd.Parameters.AddWithValue("@eid", TextBox1.Text.Trim());
    cmd.Parameters.AddWithValue("@ename", TextBox2.Text.Trim());
    cmd.Parameters.AddWithValue("@eaddress", TextBox3.Text.Trim());
    cmd.Parameters.AddWithValue("@epanno", TextBox4.Text.Trim());
    cmd.Parameters.AddWithValue("@epassword", TextBox5.Text.Trim());
    cmd.Parameters.AddWithValue("@ecpassword", TextBox6.Text.Trim());
    cmd.Parameters.AddWithValue("@mobileno", TextBox7.Text.Trim());
    cmd.Parameters.AddWithValue("@email", TextBox9.Text.Trim());


    cmd.Parameters.AddWithValue("@pc", imgByte);
    con.Open();
    cmd.ExecuteNonQuery();
    //int id = Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();
    }
    //catch
    //{
    // Label1.Text = "There was an error";
    //}
    //finally
    //{
    // connection.Close();
    //}
    }
  • #763701
    Hi,

    If you dont want to insert image then you can remove that particular field in insert list, modify the following line of code.

    SqlCommand cmd = new SqlCommand("INSERT INTO ekthaemployee(eid,ename,eaddress,epanno,epassword,ecpassword,mobileno,email,pc) VALUES(@eid, @ename,@eaddress,@epanno,@epassword,@ecpassword,@mobileno,@email,@pc) SELECT @@IDENTITY",con);


    replace with below code

    SqlCommand cmd = new SqlCommand("INSERT INTO ekthaemployee(eid,ename,eaddress,epanno,epassword,ecpassword,mobileno,email) VALUES(@eid, @ename,@eaddress,@epanno,@epassword,@ecpassword,@mobileno,@email) SELECT @@IDENTITY",con);


    and remove the below line of code too..


    cmd.Parameters.AddWithValue("@pc", imgByte);


    once you done the above changes then execute the application again, if you still face the problem then let me know with error details.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763786
    Hai VELIDIRAVIRAM,
    If you have the Image type field in your database, then you cant insert the text for this field.
    If you want to insert the empty image you can insert as new byte[0] which is the byte array of 0.
    So if the content length of the image is 0, then you can use :

    cmd.Parameters.AddWithValue("@pc", new Byte[0]);

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments