Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I wrote some code to read an integer value from an sql server:

if (rdr.HasRows)
   {
     StringBuilder sb = new StringBuilder();
     string a;
     while (rdr.Read())
      {
        sb.Append(((int[field]).ToString());
        sb.Append(\n);
        a = sb.ToString().Trim();
        c1.Items.Insert(count1, a);
        sb.Length=0;
        count1++;
      }

   }


Now I do not understand how to read a numeric value and varchar.

Changes somewhere here:
sb.Append(((int[field]).ToString());
Posted
Updated 25-Jul-11 2:20am
v4

When you read a field from the reader, you can cast it directly to the data format you stored it as:
int i = (int) rdr["intField"];           // Field: int
string s = (string) rdr["stringField"];  // Field: varchar
byte[] b = (byte[]) rdr["imageField"];   // Field: varbinary
You do not have to convert it to a string and add it to a StringBuilder, unless that is what you actually need to do!
 
Share this answer
 
Comments
kami124 25-Jul-11 8:34am    
thanks
If I understand your question correctly, use rdr["fieldname"].ToString() to read the values from the DataReader.
 
Share this answer
 

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