Click here to Skip to main content
13,002,355 members (79,283 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Sir, I recently developed an address book application. When I try to update my address info if no new image is inserted the update query fails to execute.

private void Update_Click(object sender, EventArgs e)
if (txtfname.Text != "" && txtlname.Text != "")
// Read the Data From All fields
string fname = txtfname.Text;
string lname = txtlname.Text;
string house = txthouse.Text;
string city = txtcity.Text;
string country = txtcountry.Text;
string state = txtstate.Text;
string post = txtpost.Text;
string mail = txtmail.Text;
string blood = txtblood.Text;
string mobile = txtmobile.Text;
string dob = txtdob.Text;
byte[] imagedata = ReadFile(path.Text);
SqlConnection con = new SqlConnection(conn);
//update query
string query = "UPDATE address SET firstname ='" + fname + "',lastname='" + lname + "',house='" + house + "',city='" + city + "',country='" + country + "',state='" + state + "',post='" + post + "',mail='" + mail + "',blood='" + blood + "',mobile='" + mobile + "',dob='" + dob + "',image=@image WHERE (firstname ='" + fname + "' AND lastname='" + lname + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add(new SqlParameter("@image", (object)imagedata));
int i;
i = cmd.ExecuteNonQuery();
if (i == 1)
rInsert.Text = "Address Updated Sucessfully...!!!";
rInsert.Text = "Name not found in Database...!!!";

catch (Exception)
MessageBox.Show("Unable to Update...!!!", "Error occur", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
MessageBox.Show("Please Insert first name and last name"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//coverting image to bytes for storing in data base
byte[] ReadFile(string sPath)
//Initialize byte array with a null value initially.
byte[] data = null;

//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;

//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);

//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
Posted 4-Oct-12 1:19am
Updated 5-Oct-12 7:21am
Wes Aday 4-Oct-12 7:41am
Sorry that you are having trouble. But thanks for keeping us updated with your progress. Please feel free to come back if you happen to have a question to ask.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The most likely reason is that your image column in the DB is set as "allow nulls" false.
If you do not provide an image with each new row you try to create, the SQL engine will not allow the row to be created, and will throw an exception.

Fixes are obvious:
1) Provide an image (even if it is a generic "no image" image)
2) Change the table definition to allow nulls for the image data.

However, be aware that images can get quite large, and unless you are careful how you handle them you can both exceed database size limits and slow your application to a crawl very easily. If you are storing a lot of images, I would keep them on an HDD and store the path in the DB.

[edit]Typos - OriginalGriff[/edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170626.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100