0

I am storing images to the database in the table test (id, name, image), by reading images from a picture box.

This is my code:

private void browse_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "(*.BMP;*.JPG;*.GIF;*.JPEG;*.PNG)|*.BMP;*.JPG;*.GIF;*.JPEG;*.PNG";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        imgloc = openFileDialog1.FileName.ToString();
        pictureBox1.ImageLocation = imgloc;
    }
}

private void save_Click(object sender, EventArgs e)
{
    byte[] img = null;
    FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    img = br.ReadBytes((int)fs.Length);
    SqlConnection CN = new SqlConnection(constring);
    string Query = "insert into test (id,name,image) values('" + txtid.Text + "','" + txtname.Text + "',@img)";
    CN.Open();
    cmd = new SqlCommand(Query, CN);
    cmd.Parameters.Add(new SqlParameter("@img", img)); 
    cmd.ExecuteNonQuery();
    CN.Close();
}

It works but I would like to know how to use the update command here.

2
  • instead of insert use UPDATE query Commented Jul 25, 2013 at 11:26
  • What do you mean with "update command"? Commented Jul 25, 2013 at 11:27

4 Answers 4

3
private void update_Click(object sender, EventArgs e)
    {
        byte[] img = null;
        FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        img = br.ReadBytes((int)fs.Length);
        SqlConnection CN = new SqlConnection(constring);

        // this is a smaple query for update statement and update where id=@id
        string Query = "update test set name=@name,image=@img where id=@id ";

        CN.Open();
        cmd = new SqlCommand(Query, CN);
        cmd.Parameters.Add(new SqlParameter("@img", img));
        cmd.Parameters.Add(new SqlParameter("@id", txtid.Text));
        cmd.Parameters.Add(new SqlParameter("@name", txtname.Text));
        cmd.ExecuteNonQuery();
        CN.Close();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Your code and query should be like this :

SqlConnection CN = new SqlConnection(constring);
string Query = "Update test Set name=@Name,image=@Image where id=@id"
CN.Open();
cmd = new SqlCommand(Query, CN);
cmd.Parameters.Add(new SqlParameter("@Image", img));
cmd.Parameters.Add(new SqlParameter("@Name",txtname.Text));
cmd.Parameters.Add(new SqlParameter("@id",txtid.Text));
cmd.ExecuteNonQuery();
CN.Close();

Comments

0

Just delete that particular record using your Id field and Fire the Save query again, if updating is difficult.

Comments

0
 SqlConnection con = Connectionclass.SQLCONNECTION();
 SqlDataAdapter da = new SqlDataAdapter();
 string query = ("Update Doctor set ID ='" + idtxt.Text + "',Name='" + nametxt.Text + "',Contact='" + contactxt.Text + "',CNIC='" + cnictxt.Text + "',Address='" + addresstxt.Text + "',Qualification='" + qualitxt.Text + "',specialization='" + specialtxt.Text + "',Gender='" + gendertxt.Text + "',DOB='" + dobtxt.Text + "', Fee='"+textBox1.Text+"',Date='" + System.DateTime.Today.ToString("dd-MM-yyyy") + "', Picture= @image  where ID='" + idtxt.Text + "'");
 da.UpdateCommand = new SqlCommand(query, con);
 con.Open();
 da.UpdateCommand.Parameters.Add("image", SqlDbType.VarBinary).Value = binaryphoto;
 int RowsEffected = da.UpdateCommand.ExecuteNonQuery();
 con.Close();

1 Comment

Hi there, thanks for your answer. Please try explain your solution a little better with the steps you followed to solve the problem. Additionally, when pasting in code, remember to format it with Ctrl+K or backticks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.