how to apply paramatized queeries with this code

Last post 07-07-2009 6:07 PM by Mikesdotnetting. 16 replies.

Sort Posts:

  • how to apply paramatized queeries with this code

    07-02-2009, 11:10 PM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     On my class module:

    Public Sub OptListTwoVal(ByVal List As Object, ByVal sQuery As String, ByVal sConn As String)
        Using LibConn As New System.Data.SqlClient.SqlConnection(sConn)
    
          LibConn.Open()
                Dim cu As New System.Data.SqlClient.SqlCommand(sQuery, LibConn)
                Dim cr As System.Data.SqlClient.SqlDataReader = cu.ExecuteReader()
          With cr
            If .HasRows Then
              While .Read
                List.Items.Add(New ListItem(.Item(1).ToString, .Item(0).ToString))
              End While
            End If
          End With
        End Using
      End Sub

    On my Page:
     

    I am adding a parameter like this

                LibMain.sSelect = "EXEC spt_PhysiciansModule"
                Try
                     LibMain.AddParameter("@QType", SqlDbType.Char, "A", , ParameterDirection.Input)
                    LibMain.AddParameter("@Desc1", SqlDbType.Char, "", , ParameterDirection.Input)
                    LibMain.AddParameter("@Desc2", SqlDbType.Char, "", , ParameterDirection.Input)
                    LibMain.OptListTwoVal(ddlPhyType, LibMain.sSelect, LibMain.connClaims)
                Catch ex As Exception

    The error msg is

    {"Procedure or Function 'spt_PhysiciansModule' expects parameter '@Qtype', which was not supplied."}

    whats wrong with my code? please help..


     

    ez
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 3:01 AM

    You need to provide a value for the parameter:

    Dim sQuery As String = "spt_PhysiciansModule"
    Dim cu As New SqlCommand(sQuery, sConn)
    cu.CommandType = CommandType.StoredProcedure
    cu.Parameters.AddWWithValue("QType", somevalue)
    cu.Parameters.AddWithValue("Desc1", someothervalue)  etc


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 3:13 AM
    • All-Star
      57,651 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,201
    • TrustedFriends-MVPs
    MAK [MVP ASP/ASP.Net]

    ASP.Net Consultant My Site : ASPSnippets

    Delete mutiple rows in GridView

    Using AsyncFileUpload Control
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 3:41 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     It doesnt work with the code above.

    ez
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 4:03 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

    Dim sQuery1 As String = "spt_PhysiciansModule"

    Dim cu As New SqlCommand(sQuery1, LibConn)

    Dim cr As System.Data.SqlClient.SqlDataReader = cu.ExecuteReader()

    cu.CommandType = CommandType.StoredProcedure

    cu.Parameters.AddWithValue("@QType", "A")

    cu.Parameters.AddWithValue("@Desc1", "")

    cu.Parameters.AddWithValue("@Desc2", "")

     

    Error Msg: Procedure or Function 'spt_PhysiciansModule' expects parameter '@Qtype', which was not supplied.

    ez
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 4:16 AM
    • Member
      24 point Member
    • cshark
    • Member since 07-02-2009, 4:50 PM
    • Rochester
    • Posts 13

    You are adding the parameter as @QType in .net where as the parameter in sql is @Qtype. Smile


    try the below line.

    cu.Parameters.AddWithValue("@Qtype", "A")

  • Re: how to apply paramatized queeries with this code

    07-03-2009, 4:27 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     whats the difference?

    mine is

    cu.Parameters.AddWithValue("@QType", "A")

    ez
  • Re: how to apply paramatized queeries with this code

    07-03-2009, 4:30 AM
    • Member
      24 point Member
    • cshark
    • Member since 07-02-2009, 4:50 PM
    • Rochester
    • Posts 13

    Command parameters in .net are case sensitive. @QType is different from @Qtype

  • Re: how to apply paramatized queeries with this code

    07-03-2009, 11:51 AM
    • Star
      8,276 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,421

    cshark:
    Command parameters in .net are case sensitive
     

    I don't think it is .NET that is case sensitive. I think the sensitive Collation setting of the SQL Server requires the parameters to be case sensitive....

  • Re: how to apply paramatized queeries with this code

    07-03-2009, 12:11 PM
    • Contributor
      2,630 point Contributor
    • RichardD
    • Member since 09-03-2002, 11:43 AM
    • Sussex, UK
    • Posts 346

    The problem is that you are creating a 'Text' command rather than a 'StoredProcedure' command. If you set cu.CommandType to CommandType.StoredProcedure and change the query to "spt_PhysiciansModule", the query will work.

    Alternatively, if you can't change the command type, you need to specify the parameters in the query:

    EXEC spt_PhysiciansModule @QType = @QType, @Desc1 = @Desc1, @Desc2 = @Desc2

    The names need to be specified twice so that SQL matches the parameters by name; if you just specified "EXEC spt_PhysicialsModule @QType, @Desc1, @Desc2", the parameters would be matched by position, which may not be correct.

  • Re: how to apply paramatized queeries with this code

    07-06-2009, 2:53 AM
    Answer
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     ok got it

    if modified my class

    .CommandType = CommandType.StoredProcedure
    .CommandTimeout = 4000
    .Parameters.AddWithValue("QType", P1)
    .Parameters.AddWithValue("Desc1", P2)
    .Parameters.AddWithValue("Desc2", P3)


     

    ez
  • Re: how to apply paramatized queeries with this code

    07-06-2009, 3:00 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     but i wonder if how can I make my parameters flexible.  in a way for example the stored proc that will be use has 4 parameters, since my code(class) has only designed for 3 parameters only.  how can I accomplished this?

     

    ez
  • Re: how to apply paramatized queeries with this code

    07-06-2009, 8:16 AM
    Answer

    Here's an example:

    public static void ExecNonQuery(string proc, string[] Params, Object[] Values)
    {
        using (SqlConnection conn = new SqlConnection(connstring))
        {
            SqlCommand cmd = new SqlCommand(proc, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Params.Length; i++)
            {
                cmd.Parameters.AddWithValue(Params[i], Values[i]);
            }
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }


    This one causes an ExecuteNonQuery() command to be executed.  You just feed it a procedure name, an array of strings for the parameter names and an array of objects that provide the values to the parameters. 

    Eg:

    string sql = DeleteAllExpired; // name of procedure
    string[] p = new string{"id"};
    object[] o = new object{TextBox1.Text};
    ExecNonQuery(sql, p, o);


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: how to apply paramatized queeries with this code

    07-07-2009, 4:10 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     I've tried converting it to VB

    from

    {
        using (SqlConnection conn = new SqlConnection(connstring))
        {
            SqlCommand cmd = new SqlCommand(proc, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Params.Length; i++)
            {
                cmd.Parameters.AddWithValue(Params[i], Values[i]);
            }
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    To

    Public Shared Sub ExecNonQuery(proc As String, Params As String(), Values As [Object]())
    	Using conn As New SqlConnection(connstring)
    		Dim cmd As New SqlCommand(proc, conn)
    		cmd.CommandType = CommandType.StoredProcedure
    		Dim i As Integer = 0
    		While i < Params.Length
    			cmd.Parameters.AddWithValue(Params(i), Values(i))
    			System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    		End While
    		conn.Open()
    		cmd.ExecuteNonQuery()
    	End Using
    End Sub

    -------------------------------------

    From

    string sql = DeleteAllExpired; // name of procedure
    string[] p = new string{"id"};
    object[] o = new object{TextBox1.Text};
    ExecNonQuery(sql, p, o);

    To

    Dim sql As String = DeleteAllExpired
    ' name of procedure
    Dim p As String() = New String()
    Dim o As Object() = New Object()
    ExecNonQuery(sql, p, o)
    

    Are these correct?



    ez
  • Re: how to apply paramatized queeries with this code

    07-07-2009, 5:29 AM
    • Participant
      1,086 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 594

     The problem is, the parameter in the stored procedure should be the same with the parameter declared in .net

    ez
Page 1 of 2 (17 items) 1 2 Next >