none
how do i get a column data RRS feed

  • Question

  • i did this :

    Dim sql As String = "SELECT User_Pass FROM TblUsers Where User_name='" & user & "'"

    'Create a SqlCommand instance

    Dim myCommand As New SqlCommand(sql, myConnection)

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

     

    how can get the User_Pass value?

    currently i recive and error

     

    thnaks

    peleg

    Saturday, July 7, 2007 9:59 PM

All replies

  • What error message are you getting?
    Sunday, July 8, 2007 12:49 AM
  • Did you call Read method of SqlDataReader before you started to read values? Here is sample how to use DataReader

    http://support.microsoft.com/kb/308656/en-us

    Monday, July 9, 2007 10:46 AM
    Moderator
  • You are almost there

    Dim sql As String = "SELECT User_Pass FROM TblUsers Where User_name='" & user & "'"

    Dim myCommand As New SqlCommand(sql, myConnection)

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

     

    // C# Style

    while ( SqlDataReader.Read()) {

     //Since you select one 1 column in your Sql Statement,

    // then you need to use 0 index to get your  data.

    Debug.WriteLine( SqlDataReader[0]);

     

    }

     

     

     

     

     

    Tuesday, July 10, 2007 1:18 PM
  • Code Snippet

    string sql;

    SqlCommand myCommand;

    SqlDataReader reader;

    SqlParameter parameter;

    string password;

     

    sql = "SELECT USER_PASS FROM TBLUSERS WHERE USER_NAME = @User";

     

    using(myCommand = new SqlCommand(sql,myConnection)) {

     

        parameter = new SqlParameter("@User",user);

        command.Parameters.Add(parameter);

     

        using(reader = myCommmand.ExecuteReader()) {

     

            if(reader.Read()) {

                password = (string)reader["USER_PASS"];

            } else {

                password = null;

            }

     

            reader.Close();

     

        }

     

    }

     

    I am assuming that 'user' and 'myConnection' are properly initialized.  It is also good practice to make use of 'using' statements when dealing with types of IDisposable and to avoid string concatenation when creating queries.  Parameterization will help deter SQL injection in your queries.

    Thursday, July 12, 2007 8:42 PM