Click here to Skip to main content
15,885,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<pre lang="cs">con.Open();
SqlCommand cmd = new SqlCommand("insert into Exe_ContactDetails values(@Name,@Mobile,@Address,@UserId)", con);
cmd.CommandType = CommandType.Text;
string str = "Select ID from Team_Registration where UserName= '" + Session["New"] + "'";
//int N = Convert.ToInt32(str);
cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
cmd.Parameters.AddWithValue("@UserId", str);
cmd.ExecuteNonQuery();
con.Close();</pre>

when I executed this code (Conversion failed when converting the nvarchar value 'Select ID from Team_Registration where UserName= 'madhu'' to data type int.)getting this error.
Please suggest to me how can I errase this error.

How can I store the Team_registration ID in another table column UserId

Please give me a answer.....
Posted

1 solution

change the sql statement as below
C#
SqlCommand cmd = new SqlCommand("insert into Exe_ContactDetails values(@Name,@Mobile,@Address,(Select ID from Team_Registration where UserName=@username))", con);

now you can set the username parameter
C#
cmd.Parameters.AddWithValue("@username", Session["New"].ToString());
 
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