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

    How to save values into database without using image to save into datbase

    hi all i have a small requrement in my project like saving employee details into datbase
    some employee photos we have but for some employee photos we donot have .so what i want is to save textboxvalues and image uploaded from file upload control to database if i have emp details with image .
    if i have no image but only the employee details so only my emp details to save
    below is my code where i am wrong plz helpme

    protected void Button1_Click(object sender, EventArgs e)
    {


    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);
    // cmd.Parameters.AddWithValue("@pc", 0);
    con.Open();
    cmd.ExecuteNonQuery();
    //int id = Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();
    }
    //catch
    //{
    // Label1.Text = "There was an error";
    //}
    //finally
    //{
    // connection.Close();
    //}
    }
  • #763703

    Hi,

    In that case the image field should accept null values too... Please check the field type and nullable case in database, if it's accept null's then there is no issues, if you still having problem please share the error details.


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

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

  • #763766

    Hai VELIDIRAVIRAM,
    You cant set the null value for the Image column in the database but you can have the 0 bytes to be stored in the database Image field.
    So in your code you need to make a modifications like:

    //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
    if(File.ContentLength ==0)
    {
    File.InputStream.Read(new Byte[0], 0, 0);
    }
    else
    File.InputStream.Read(imgByte, 0, File.ContentLength);

    It will save the 0 byte array in to the database when there is no image available.
    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