Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the problem is this i have TextBoxes and m trying to convert their value to int by using this code:
C#
SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into yearly (pcode,fyyear,yearlyalloc,salary,ta,contigency,nrc,institcharges,others) values (@pcode,@fyyear,@yearlyalloc,@salary,@ta,@contigency,@nrc,@institcharges,@others)";
        cmd.Parameters.AddWithValue("@pcode", DropDownList1.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@fyyear", TextBox10.Text +"-"+ TextBox2.Text);
        cmd.Parameters.AddWithValue("@yearlyalloc",Convert.ToInt32(TextBox3.Text));
        cmd.Parameters.AddWithValue("@salary", int.Parse(TextBox4.Text));
        cmd.Parameters.AddWithValue("@ta", int.Parse(TextBox5.Text));
        cmd.Parameters.AddWithValue("@contigency", int.Parse(TextBox6.Text));
        cmd.Parameters.AddWithValue("@nrc", int.Parse(TextBox7.Text));
        cmd.Parameters.AddWithValue("@institcharges", int.Parse(TextBox8.Text));
        cmd.Parameters.AddWithValue("@others", int.Parse(TextBox9.Text));

but i am getting error "Input string was not in a correct format" as i was told that i cant leave a textbox blank with convert.toint32 statement. so i wanna use int.TryPrase statement for inserting data.
Posted
Updated 29-Oct-12 2:04am
v2
Comments
vivektiwari97701 29-Oct-12 7:08am    
use try.... catch and find where exact That error is throwing exception and 1 more thing can u show table detail like datatype of columns ...
a2ulthakur 29-Oct-12 7:21am    
first 2 textboxes are varchar and rest all are int
vivektiwari97701 29-Oct-12 7:33am    
if U want All that field would be Mandatory then Just Use Javascript validation Or Other Validation Methods.then Save Ur Data In This Case U have To enter correct Value For All Fields and U can use TryParse or Convert or Parse it will not thorow error . U Can Google it with keyword Client Side Validation/Server side validation in ASP.net..etc....

1 solution

I said to check it first using tryParse: i m getting an error "Input string was not in a correct format.". i am not able to figure out y ?[^] not to replace the Convert.ToInt32 with int.Parse

For example:
C#
int salary;
if (!int.TryParse(TextBox4.Text, out salary))
   {
   // Report error
   return;
   }
...
   cmd.Parameters.AddWithValue("@salary", salary);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900