-4

I have a SQL Server 2008 database and I am working on it in the backend. I am working on asp.net/C#

using (SqlCommand StrQuer = new SqlCommand("SELECT *  FROM [shopcart].[dbo].[user] WHERE username=@userid AND password=@password", myconn))
{
   StrQuer.Parameters.AddWithValue("@userid", str_usr);
   StrQuer.Parameters.AddWithValue("@password", str_psd);

   SqlDataReader dr = StrQuer.ExecuteReader();

   if (dr.HasRows)
   {
      // MessageBox.Show("loginSuccess");    
   }
}

I know that the reader has values. My SQL command is to select just 1 row from a table. I want to read the items in the row in the reader one by one. How do I do this?

Thanks

2
  • simply use dr["columnname"] Commented Aug 1, 2013 at 10:58
  • such type of questions i dont think should be answered here, very basic thing one can get answer at first search Commented Aug 1, 2013 at 10:59

6 Answers 6

2

You may use the FieldCount property:

if (dr.Read())
{
    for (int i = 0; i < dr.FieldCount; i++)
    {
        var value = dr[i];
        // do something
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

1

You can use dr.Read() inside while loop. Like This :

while(dr.read())
{
    // MessageBox.Show("loginSuccess");    
}

Comments

1
if (dr.HasRows)
{
    dr["NameOfColumn"].ToString();    
}

You obviously assign the variable and cast to appropriate type above.

Take a look at my question here too, for another solution Casting Ado.net DataReader to IDataRecord giving strange result

Comments

1

You can use dr.Read() inside while loop.

while(dr.Read())
{
    string firstCell = dr[0].ToString();
    string secondCell = dr[1].ToString();

    // and so on...
}

// It will be better if you close DataReader
dr.Close();

1 Comment

0

Here is full example of sqldatareader.

 SqlConnection myConnection = new SqlConnection();  
        myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;  
        SqlCommand myCommand = new SqlCommand();  
        myCommand.CommandType = CommandType.Text;  
        myCommand.CommandText = "SELECT CategoryID, CategoryName FROM Categories";  
        myCommand.Connection = myConnection;  
        SqlDataReader myReader;  

        try  
        {  
            myConnection.Open();  
            myReader = myCommand.ExecuteReader();  
            while (myReader.Read())  
            {  
                ListItem li = new ListItem();  
                li.Value = myReader["CategoryID"].ToString();  
                li.Text = myReader["CategoryName"].ToString();  
                ListBox1.Items.Add(li);  
            }  
            myReader.Close();  
        }  
        catch (Exception err)  
        {  
            Response.Write("Error: ");  
            Response.Write(err.ToString());  
        }  
        finally  
        {  
            myConnection.Close();  
        }  

1 Comment

This is a terrible example. There are no less than 3 IDisposable resources here and you Dispose none of them. Please read the fine manual for the using statement.
0

Put the name of the column begin returned from the database where "ColumnName" is. If it is a string, you can use .ToString(). If it is another type, you need to convert it using System.Convert.

SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string column = rdr["ColumnName"].ToString();
int columnValue = Convert.ToInt32(rdr["ColumnName"]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.