Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 4-Oct-12 1:19am
Edited 5-Oct-12 7:21am
v3
Comments
Wes Aday at 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
good
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]
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 585
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,295
1 OriginalGriff 6,841
2 CPallini 5,420
3 George Jonsson 3,654
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
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