Solved

Input string was not in a correct format

Posted on 2013-05-17
1
463 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:
Tchuki 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now