I have following code its shows an error please some one find what mistake I have done.
Error at 9th line as "Input string was not in a correct format".
private void button2_Click(object sender, EventArgs e)
{
int CustomerId=0;
int i=0;
SqlConnection con = new SqlConnection(@"server=CSE-PC; integrated security=true; Database=Bas");
con.Open();
SqlCommand com = new SqlCommand("select max(CustomerId) FROM DEVI", con);
CustomerId = int.Parse(com.ExecuteScalar().ToString());
/*Input string was not in a correct format.*/
if (i>0)
{
CustomerId = CustomerId + 1;
}
else
{
CustomerId = 001;
}
comboBox1.Items.Add("CustomerId");
i = i + 1;
MessageBox.Show("IdCreated");
con.Close();
}
Loading

Syed ShanuPosted Apr 13, 2015, 2:16 AM
try the below code.
int CustomerId = 0;
int i = 0;
SqlConnection con = new SqlConnection(@"server=CSE-PC; integrated security=true; Database=Bas");
con.Open();
SqlCommand com = new SqlCommand("select max(CustomerId) FROM DEVI", con);
if (com.ExecuteScalar().ToString() == "")
{
CustomerId = 0;
}
else
{
CustomerId = int.Parse(com.ExecuteScalar().ToString());
}
if (CustomerId > 0)
{
CustomerId = CustomerId + 1;
}
else
{
CustomerId = 001;
}
Durga VelusamyPosted Apr 13, 2015, 3:51 AM
Ankur JainPosted Apr 13, 2015, 2:17 AM
I am not sure why you would get this in v2.0 but not in previous versions. ExecuteScalar() is returning something that cannot be cast to an int, and thus you are seeing this error. So the first thing you can check is what ExecuteScalar is really returning. You can use the debugger, or output "ExecuteScalar().GetType().ToString()", and that will give you the type of what is being returned.
Michal HabalcikPosted Apr 13, 2015, 2:14 AM
CustomerId = int.Parse(com.ExecuteScalar().ToString());
put a break point there and check what is the ExecuteScalar actually returning.
If you are not really familiar with debuging, just print it to console to check out:
Console.WriteLine(com.ExecuteScalar().ToString())