Solved

Input string was not in a correct format

Posted on 2013-05-17
1
474 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to pass stored proc parameters in list 13 33
Filename to be appended with DateTimeof Download 3 46
SQL Exceptions 3 38
C# Comparing String Arrays. 5 25
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

13 Experts available now in Live!

Get 1:1 Help Now