Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi dear's,
this is my code:
C#
SqlDataReader dReader;
        SqlConnection conn = new SqlConnection(@"Data Source=.\sql2008;Initial Catalog=jelvehsadb;Integrated Security=True;");
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;

        comm.CommandText = @"Select response from work_with_us where national_code =@CustomerID";
        comm.Parameters.AddWithValue("CustomerID", 00488);  
        comm.Connection.Open();
        

        dReader = comm.ExecuteReader();
          
        if (dReader.HasRows == true)
        {
            TxtShow.Text = ???;
        }
        

    }

my problem is how i can get my response vlaue from dreader??
the result must show on txtshow.
thanks
Posted

Try:
C#
if (dReader.Read())
   {
   TxtShow.Text = (string) reader["response"];
   }



"but in step 2 when my sqlcommand return a value,it's not related to the response column,
so i want access to that value is now ready on the datareader,may you help me how i can access to this data,
thanks dera OriginalGriff"



Yes, it is!
When your issue a SELECT, SQL returns all the rows that match the WHILE statement, but only the columns you specify. So if you have a table with three columns:
Id   UserName  UserAddress
7    Fred      Home
8    Joe       Away
9    Bert      Holiday cottage
And you issue a SELECT:
SELECT UserName, UserAddress FROM MyTable WHERE Id=8

Then you need to do two accesses to your DataReader to read each of the two columns of the row that you have asked for:
C#
string userName = (string) reader["UserName"];
string userAddr = (string) reader["UserAddress "];


In your case, you are asking for (I assume) a single row (where the national_code is 488) and a single column: response.

Does that make sense? Or are we talking at cross purposes here?
 
Share this answer
 
v2
Comments
Member 8453437 24-Sep-13 4:10am    
what is response???
response is not the function of sqldatareader,
this code return a value from sql,
i want access to that value.
thank's
OriginalGriff 24-Sep-13 4:18am    
If you look closely, you will see that "response" is a string: it is the name of the column that you requested SQL to return:
comm.CommandText = @"Select response from work_with_us where national_code =@CustomerID";
So when you want the value, you retrieve that column by name:
TxtShow.Text = (string) reader["response"];
(you can access it by number, but that is not such a good practice in the real world)
Member 8453437 24-Sep-13 4:32am    
you are right,in first step i need to the name of the column,
but in step 2 when my sqlcommand return a value,it's not related to the response column,
so i want access to that value is now ready on the datareader,may you help me how i can access to this data,
thanks dera OriginalGriff
OriginalGriff 24-Sep-13 4:41am    
Answer updated
Member 8453437 24-Sep-13 4:48am    
thanks dear OriginalGriff,
i dot wrote below code,so i can't get result,
dReader.Read();
thanks for your guide.
Hello Member,

You can modify your code as shown below.
C#
SqlDataReader dReader;

SqlConnection conn = new SqlConnection(@"Data Source=.\sql2008;Initial Catalog=jelvehsadb;Integrated Security=True;");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
 
comm.CommandText = @"Select response from work_with_us where national_code =@CustomerID";
comm.Parameters.AddWithValue("CustomerID", 00488);  
comm.Connection.Open();
        
dReader = comm.ExecuteReader();
// The default position of the SqlDataReader is before the first record. 
// Therefore, you must call Read to begin accessing any data.
if (dReader.Read()) {
    TxtShow.Text = dReader.GetString(0);
}

Regards,
 
Share this answer
 
Comments
Member 8453437 24-Sep-13 4:17am    
i get this error:
Invalid attempt to read when no data is present.
how i can present data??
thanks
you can do something like this :
C#
SqlDataReader drmax1;
 drmax = comm.ExecuteReader();
                    drmax.Read();
                    TxtShow.Text = System.Convert.ToString((System.Convert.ToInt32(drmax["response"].ToString()));
 
Share this answer
 
Comments
Member 8453437 24-Sep-13 4:19am    
thanks but i get below error
Input string was not in a correct format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900