Solved

Input string was not in a correct format

Posted on 2013-05-17
1
489 Views
Last Modified: 2013-05-17
Hi,
When running the below codes,
                try
                {
                    cmd = new SqlCommand("insert into dbo.tab3 (user_id,rec_id,rec_desc,change_date) values (@par_id2,11,' ',getdate())", conn);

                    cmd.Parameters.Add("@par_id2", SqlDbType.Int).Value = Convert.ToInt32(lb_userid.Text);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    lb_msg.Text = ex.Message + ex.Source;
                    return;
                }
                ...

I don't know why I've got this error.

Input string was not in a correct format.mscorlib
0
Comment
Question by:HuaMinChen
1 Comment
 
LVL 14

Accepted Solution

by:
Vel Eous earned 110 total points
ID: 39174000
Input string was not in a correct format

The above is telling you that your UI control did you contain a string value that it could convert to an Int.

You really want to validate the input before you attempt your process:

try
{
    int converted = 0;
    if (Int32.TryParse(lb_userid.Text, out converted))
    {
        cmd = new SqlCommand("insert into dbo.tab3 (user_id,rec_id,rec_desc,change_date) values (@par_id2,11,' ',getdate())", conn);
        cmd.Parameters.Add("@par_id2", SqlDbType.Int).Value = converted;
        cmd.ExecuteNonQuery();
    }
    else
    {
        lbl_msg.Text = "Unable to convert user id";
    }
}
catch (Exception ex)
{
    lb_msg.Text = ex.Message + ex.Source; 
}
return;

Open in new window

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question