Help with SQL Parameter
-
What am I doing wrong? SqlConnection conn = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=opc;Integrated Security=True"); conn.Open(); SqlCommand selCmd = new SqlCommand("SELECT COUNT(*) FROM data WHERE timestamp>@pdate1 AND timestamp<@pdate2", conn); SqlParameter date1 = new SqlParameter(); date1.ParameterName = @pdate1; date1.Value = TextBox1.Text; SqlParameter date2 = new SqlParameter(); date2.ParameterName = @pdate2; date2.Value = TextBox2.Text; selCmd.Parameters.Add(date1); selCmd.Parameters.Add(date2); int count = (int)selCmd.ExecuteScalar(); Label1.Text = count.ToString(); conn.Close(); I get 2 errors: Error 1 The name 'pdate1' does not exist in the current context Error 2 The name 'pdate2' does not exist in the current context
-
What am I doing wrong? SqlConnection conn = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=opc;Integrated Security=True"); conn.Open(); SqlCommand selCmd = new SqlCommand("SELECT COUNT(*) FROM data WHERE timestamp>@pdate1 AND timestamp<@pdate2", conn); SqlParameter date1 = new SqlParameter(); date1.ParameterName = @pdate1; date1.Value = TextBox1.Text; SqlParameter date2 = new SqlParameter(); date2.ParameterName = @pdate2; date2.Value = TextBox2.Text; selCmd.Parameters.Add(date1); selCmd.Parameters.Add(date2); int count = (int)selCmd.ExecuteScalar(); Label1.Text = count.ToString(); conn.Close(); I get 2 errors: Error 1 The name 'pdate1' does not exist in the current context Error 2 The name 'pdate2' does not exist in the current context
It should look like this: date1.ParameterName = "@pdate1"; date2.ParameterName = "@pdate2";
#region signature my articles #endregion
-
It should look like this: date1.ParameterName = "@pdate1"; date2.ParameterName = "@pdate2";
#region signature my articles #endregion
Yes I found it few minute later...Thanks But now I get another error. protected void Button_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=opc;Integrated Security=True"); conn.Open(); SqlCommand selCmd = new SqlCommand("SELECT COUNT(*) FROM data WHERE timestamp>@date1 AND timestamp<@date2", conn); SqlParameter param1 = new SqlParameter(); SqlParameter param2 = new SqlParameter(); param1.ParameterName = "@date1"; param1.Value = TextBox1.Text; selCmd.Parameters.Add(param1); param2.ParameterName = "@date2"; param2.Value = TextBox2.Text; selCmd.Parameters.Add(param2); int count = (int)selCmd.ExecuteScalar(); Label1.Text = count.ToString(); conn.Close(); } I get this Sql Exception: Arithmetic overflow error converting expression to data type datetime. In line "int count = (int)selCmd.ExecuteScalar();" I need to count the rows between these two dates...
-
Yes I found it few minute later...Thanks But now I get another error. protected void Button_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=opc;Integrated Security=True"); conn.Open(); SqlCommand selCmd = new SqlCommand("SELECT COUNT(*) FROM data WHERE timestamp>@date1 AND timestamp<@date2", conn); SqlParameter param1 = new SqlParameter(); SqlParameter param2 = new SqlParameter(); param1.ParameterName = "@date1"; param1.Value = TextBox1.Text; selCmd.Parameters.Add(param1); param2.ParameterName = "@date2"; param2.Value = TextBox2.Text; selCmd.Parameters.Add(param2); int count = (int)selCmd.ExecuteScalar(); Label1.Text = count.ToString(); conn.Close(); } I get this Sql Exception: Arithmetic overflow error converting expression to data type datetime. In line "int count = (int)selCmd.ExecuteScalar();" I need to count the rows between these two dates...
It means that when date1 and date2 are replaced with values from the textbox they are not formated correctly and inside the query they can't be compared. Try running the query manually in sql server to see what error you will get. Also, if I remember it correctly, datetime variables need ' in the query
#region signature my articles #endregion
-
It means that when date1 and date2 are replaced with values from the textbox they are not formated correctly and inside the query they can't be compared. Try running the query manually in sql server to see what error you will get. Also, if I remember it correctly, datetime variables need ' in the query
#region signature my articles #endregion
I tried the below query SELECT COUNT(*) AS Expr1 FROM data WHERE (timestamp > 19 / 08 / 2007) AND (timestamp < 23 / 08 / 2007) manually but got 0 The column is 19/8/2007 10:25:40 am 19/8/2007 10:25:59 am 20/8/2007 6:10:56 am 20/8/2007 6:11:49 am 20/8/2007 6:20:20 am 20/8/2007 6:20:32 am 20/8/2007 6:20:38 am
-
I tried the below query SELECT COUNT(*) AS Expr1 FROM data WHERE (timestamp > 19 / 08 / 2007) AND (timestamp < 23 / 08 / 2007) manually but got 0 The column is 19/8/2007 10:25:40 am 19/8/2007 10:25:59 am 20/8/2007 6:10:56 am 20/8/2007 6:11:49 am 20/8/2007 6:20:20 am 20/8/2007 6:20:32 am 20/8/2007 6:20:38 am
It means that you have mistake in your sql query
#region signature my articles #endregion