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.