Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Input string was not in a correct format

Posted on 2013-05-17
1
Medium Priority
?
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
Vel Eous earned 440 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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