I want to retrieve data from db table column one by one.
Suppose, I have this table:
**Column_Name=Name**
Mark
David
Bob
Bond
Smith
I am using this code to retrive data one by one form db table column.
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Odisha1ConnectionString2"].ConnectionString);
conn.Open();
String query = "select Name from Customer";
SqlCommand cmd2 = new SqlCommand(query, conn);
SqlDataReader dataReader = cmd2.ExecuteReader();
while (dataReader.Read())
{
string Col0 = dataReader.GetValue(0).ToString();
//string Col1 = dataReader.GetValue(1).ToString();
//string Col2 = dataReader.GetValue(2).ToString();
//string Col3 = dataReader.GetValue(3).ToString();
//string Col4 = dataReader.GetValue(4).ToString();
Response.Write("col0="col0);
}
But using this code, all data is coming in one row.I am getting output like this:
MarkDavidBobBondSmith
I want to assign all data one by one in string.I like to get output like this as i am using in my code above.
col0 = Mark
col1 = David
col2 = Bob
col3 = Bond
col4 = Smith
dateReader.FieldCountin a loop to write each column value to the response, egfor(int i=0;i<reader.FieldCount;i++){ var value=dataReader.GetValue(0); Response.Write("col{0} = {1}",i,value);}{brackets of the while loop will run once for each of the five rows in your table so perhaps you'd be better off adding them to a list: stackoverflow.com/a/1370592/825093. The reason there are no spaces is because you need to have a newline character at the end of the string you use in your Response.Write call.