Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help with SQL Parameter

Help with SQL Parameter

Scheduled Pinned Locked Moved C#
helpdatabasesecurityquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kallileo
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • K kallileo

      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

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      It should look like this: date1.ParameterName = "@pdate1"; date2.ParameterName = "@pdate2";

      #region signature my articles #endregion

      K 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        It should look like this: date1.ParameterName = "@pdate1"; date2.ParameterName = "@pdate2";

        #region signature my articles #endregion

        K Offline
        K Offline
        kallileo
        wrote on last edited by
        #3

        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...

        G 1 Reply Last reply
        0
        • K kallileo

          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...

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          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

          K 1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            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

            K Offline
            K Offline
            kallileo
            wrote on last edited by
            #5

            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

            G 1 Reply Last reply
            0
            • K kallileo

              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

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              It means that you have mistake in your sql query

              #region signature my articles #endregion

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups