TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jieha Lee
1.5k
186
9k
How to get id from other table and insert in other table
Jan 28 2018 9:25 PM
How to I get id from 'Admin' table based on username(which is Session["admin"]) to insert in 'Message' table? I got the error as below when I use SqlDataReader to detect the id.
The error:
System.Data.SqlClient.SqlException: Must declare the scalar variable "@tel_no".
Here is the code that I use:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
SqlConnection con =
new
SqlConnection(@
"Data Source=USER1-PC\SQLEXPRESS;Integrated Security=true;Database=STAFF_TEST"
);
con.Open();
SqlCommand getName =
new
SqlCommand(
"select id, customer_id from Admin"
, con);
SqlDataReader name = getName.ExecuteReader();
SqlCommand cmd =
new
SqlCommand(
"insert into Message(tel_no, message, timestamp, staff_id) values(@tel_no, @message, @timestamp, @staff_id)"
, con);
if
(name.Read())
{
if
(name[
"customer_id"
].Equals(Session[
"admin"
].ToString()))
{
cmd.Parameters.Add(
"@tel_no"
, SqlDbType.NVarChar).Value = Request.Form[
"offcell"
];
cmd.Parameters.Add(
"@message"
, SqlDbType.NVarChar).Value = Request.Form[
"message"
];
cmd.Parameters.Add(
"@timestamp"
, SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add(
"@staff_id"
, SqlDbType.NVarChar).Value = name[
"id"
];
}
name.Close();
}
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(
"Default.aspx"
);
}
Thanks.
Reply
Answers (
2
)
How to debug a WndProc function with multiple listview ?
Method invoking