Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello in my project am updating the image by replacing the existing image .When i tried to update it by Student Id (SID) (debugging) it shows an error that "input string was not in a correct format".How to solve this?

My Code
C#
private void btn_Update_Image_Click(object sender, EventArgs e)
        {
            try
            {
                

                if (txtSearchID.Text == "")
                {
                    errorProvider1.SetError(txtsid, "Enter a valid Student Id ");
                    txtsid.Focus();
                    return;
                }




                string connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
                Image_SqlCo = new SqlConnection(connectionstring);
                this.Image_SqlCo.Open();
                if (Image_sqlcmd.Parameters.Count == 0)
                {
                    this.Image_sqlcmd.CommandText = "UPDATE tbl_ImageData SET Picture=@Picture where SID=@SID";
                    this.Image_sqlcmd.Parameters.Add("@SID", System.Data.SqlDbType.Int, 4);
                    //this.Image_sqlcmd.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 50);
                   this.Image_sqlcmd.Parameters.Add("@Picture", System.Data.SqlDbType.Image);

                }
                this.Image_sqlcmd.Parameters[Convert.ToInt32("@SID")].Value =Convert.ToInt32(this.txtsid.Text);
                //this.Image_sqlcmd.Parameters["@Name"].Value = this.txtImagePath.Text;
                this.Image_sqlcmd.Parameters["@Picture"].Value = this.m_barrImg;
                Image_sqlcmd.Connection = Image_SqlCo;
                int iResult = this.Image_sqlcmd.ExecuteNonQuery();
                MessageBox.Show("Photo Updated");
                //btn_SaveImage.Enabled = false;
                //btn_Upload.Enabled = false;
            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.ToString());
            }

            finally
            {
                this.Image_SqlCo.Close();
            }
        }
Posted
Comments
♥…ЯҠ…♥ 12-Nov-13 0:17am    
In which line you got this error?

Hi,

You are trying to convert string value to Int32, that is not quite possible.

For example see this snipper

C#
string temp = string.Empty;
temp = temp + " Test string ";
int val = Convert.ToInt32(temp); //Here you will get the error

In your code, problematic line is
this.Image_sqlcmd.Parameters[Convert.ToInt32("@SID")].Value =Convert.ToInt32(this.txtsid.Text);


So you have to decide should I need to convert it to Int or not....
Your call.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
Member 10370658 12-Nov-13 1:54am    
hello i have changed my code
string SID = txtsid.Text;
this.Image_sqlcmd.Parameters.Add("@SID",SID);
this.Image_sqlcmd.Parameters.Add("@Picture", System.Data.SqlDbType.Image);

now its working fine....Noconversion problem..:)
♥…ЯҠ…♥ 12-Nov-13 2:29am    
Am glad its working fine.....
The error is pretty self explanatory. Basically the exception you are getting means the value you are trying to convert is not accepted. So here is an example to clarify it for you.
C#
string strInput = "12a";

If you try to convert this to a number such as:
C#
int intTest = Convert.ToInt32(strInput);

You will get the same exception. So debug your code and stop at the line where you get the exception and check the value you are getting.

Good luck,
OI
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 11-Nov-13 18:19pm    
You certainly messed up something. You can always call ToString(), for any object. Maybe you meant to write something else.
—SA
Orcun Iyigun 12-Nov-13 1:02am    
Totally right. I have corrected it. Thank you for the warning
Sergey Alexandrovich Kryukov 12-Nov-13 1:10am    
Someone already voted 1, probably before you fixed your post. Anyway, I'm glad that I decided not to down-vote it, because I did not believe that it could be your "real" mistake. I voted 4. (In particular, I think you should have mentioned Parse/TryParse methods are primary way of parsing.)
Cheers,
—SA
Orcun Iyigun 12-Nov-13 1:36am    
Thank you Sergey, I wish I can upvote your comment too. You are right about using Parse/TryParse methods.
Sergey Alexandrovich Kryukov 12-Nov-13 2:23am    
:-)

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