将ID从一个表传递到另一个表 [英] To Pass ID from the one table to another table
本文介绍了将ID从一个表传递到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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.....
推荐答案
更改sql语句如下
change the sql statement as below
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
cmd.Parameters.AddWithValue("@username", Session["New"].ToString());
这篇关于将ID从一个表传递到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文