Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to insert data from some text box to table of sqldatabase but some field is int and some one is nvarchar how to convert text of text box to them i do below code :

C#
constr = "Data Source=SAFA4-PC;Initial Catalog=StudentDb;Integrated Security=True";
 System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection();
 Conn.ConnectionString = constr;
 Conn.Open();
 System.Data.SqlClient.SqlCommand Comm = new System.Data.SqlClient.SqlCommand();
 Comm.CommandText = "insert into Tbl_Student (Code ,Name ,LastName ,CityCode ,Address ,BirthDay ,FatherName ,ID,CodeMeli,tel ) values( @Code,@Name,@LastName,@FatherName,@Id,@MeliCode,@Tel,@Address,@BirthDay,@CityCombo)";
 Comm.CommandType = System.Data.CommandType.Text;
 Comm.Connection = Conn;
 Comm.Parameters.Add("@Code", SqlDbType.Int);//, int.Parse(StudentCode.Text));
 Comm.Parameters["@Code"].Value = int.Parse(StudentCode.Text);
 Comm.Parameters.Add("@Name", SqlDbType.NVarChar);//Name.Text);
 Comm.Parameters["@Name"].Value = Name.Text;
 Comm.Parameters.Add("@LastName", SqlDbType.NVarChar);// LastName.Text);
 Comm.Parameters["@LastName"].Value = LastName.Text;
 Comm.Parameters.Add("@FatherName", SqlDbType.NVarChar);// FatherName.Text);
 Comm.Parameters["@FatherName"].Value =FatherName.Text;
 Comm.Parameters.Add("@Id", SqlDbType.Int); //int.Parse(Id.Text));
 Comm.Parameters["@Id"].Value = int.Parse(Id.Text);
 Comm.Parameters.Add("@MeliCode",SqlDbType.Int);
 Comm.Parameters["@MeliCode"].Value = int.Parse(MeliCode.Text);
 Comm.Parameters.Add("@Tel",SqlDbType.Int);
 Comm.Parameters["@Tel"].Value = int.Parse(Tel.Text);
 Comm.Parameters.Add("@Address",SqlDbType.NVarChar);
 Comm.Parameters["@Address"].Value = Address.Text;
 Comm.Parameters.Add("@BirthDay", SqlDbType.Char);// BirthDay.Text);
 Comm.Parameters["@BirthDay"].Value = BirthDay.Text.ToString();
 Comm.Parameters.AddWithValue("@CityCombo", CityCombo.ValueMember);
 Comm.ExecuteNonQuery();
 Conn.Close();

but i have this error :
Conversion failed when converting the nvarchar value 'd' to data type int.

Please help me
Tanx
Posted
Updated 11-Mar-13 23:11pm
v2

Check the values where you parse to int. One of them is a string and you are trying to convert the value to integer.
Thus you get this error.
 
Share this answer
 
Once you apply a breakpoint and debug the code, you will come to know where exactly the error is encountering.
 
Share this answer
 
The exception message is self explanatory and since we can not see your data or debug we can suggest you that the error is at one of the lines where you parse to int (totally 4 lines where you call Parse on int, where you should put the breakpoint).

Additionally you could use TryParse and assign value if it is parsed and then you will see where is your missing value (the one that makes trouble).

You can also make use of ternary operator in order not to bloat the code like this:
C#
//make temp int for parsing, just once (outside of the loop if you are looping).
int tmpInt;

//chose one of these

//assign value if successfully parsed
int.TryParse(StudentCode.Text, out tmpInt)??Comm.Parameters["@Code"].Value = tmpInt;

//or if value is not parsed assign -1
Comm.Parameters["@Code"].Value = int.TryParse(StudentCode.Text, out tmpInt)?tmpInt:-1;


Happy coding.
 
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