CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    5

    Question Parameterized query with Select Command

    string connectionString = "workstation id=AALOK;packet size=4096;user id=sa;data source=AALOK;persist security info=False;initial catalog=aks";

    conn = new SqlConnection(connectionString);
    string commandString = "SELECT QuesID, QuesDet, Quiz_Name FROM ak";

    dataAdapter = new SqlDataAdapter(commandString, conn);
    ds = new DataSet();
    dataAdapter.Fill(ds,"Ak");
    dataTable = ds.Tables["Ak"];


    I have created an application using C# 2003 with SQL Server 2000.
    The above code is working fine, but my requirement is to add "where Quiz_Name=@Quiz_Name" into the commandString and to set @Quiz_Name="Some string".
    Can anyone help me to write the correct code?

  2. #2
    Join Date
    Apr 2010
    Location
    Scotland
    Posts
    9

    Re: Parameterized query with Select Command

    Hi,

    Try something like this:

    SqlCommand cmd = new SqlCommand("SELECT QuesID, QuesDet, Quiz_Name FROM ak WHERE Quiz_Name = @Quiz_Name", conn);
    cmd.Parameters.AddWithValue("Quiz_Name", "Some string");
    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);

    Hope this helps,
    Alan
    www.rapidqualitysystems.com - Taking automated documentation to new heights

  3. #3
    Join Date
    May 2010
    Posts
    5

    Angry Re: Parameterized query with Select Command

    Thanks dear for your reply.
    I have tried the code given by you and getting error:- "System.Data.SqlClient.SqlParameterCollection' does not contain a definition for 'AddWithValue'".

    Aalok

  4. #4
    Join Date
    Apr 2010
    Location
    Scotland
    Posts
    9

    Re: Parameterized query with Select Command

    Hi Aalok,

    I don't know what version of .NET you are running, but I can only assume that the AddWithValue method is not available in your version.

    AddWithValue is not the only way to add a parameter. May I direct you towards the SqlCommand documentation: http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

    There are also some examples that you can try on this page: http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

    I hope this helps.

    Alan
    www.rapidqualitysystems.com - Taking automated documentation to new heights

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured