Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
1.00/5 (1 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 != "")
{
try
{
// 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));
con.Open();
int i;
i = cmd.ExecuteNonQuery();
if (i == 1)
{
rInsert.Text = "Address Updated Sucessfully...!!!";
}
else
{
rInsert.Text = "Name not found in Database...!!!";
}
con.Close();


}
catch (Exception)
{
MessageBox.Show("Unable to Update...!!!", "Error occur", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}
}
else
{
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
Updated 5-Oct-12 7:21am
v3
Comments
[no name] 4-Oct-12 7:25am    
Share your code please.
[no name] 5-Oct-12 13:08pm    
You should update your question by clicking on "Improve Question" and share your code there, so the codes are more readable. Then change the above comment, for eg: "See my changes".
[no name] 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

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]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900