0

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
3
  • 1
    If you wrote the output to a string file all you'd have to do is add a newline after each string. In HTML though newlines are ignored. This has almost nothing to do with how you read the data - you can use dateReader.FieldCount in a loop to write each column value to the response, eg for(int i=0;i<reader.FieldCount;i++){ var value=dataReader.GetValue(0); Response.Write("col{0} = {1}",i,value);} Commented Sep 13, 2019 at 12:22
  • The code inside the { 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. Commented Sep 13, 2019 at 12:23
  • @silleknarf I also want to store each value in different string. Commented Sep 16, 2019 at 7:28

1 Answer 1

0

You can create a counter like following:

int count = 0;    
while (dataReader.Read())
{
    string Col = dataReader.GetValue(0).ToString();
    Response.Write("col"+ count +" = "+ Col + "<br />");
    count++;
}

Update:

Added </br> at end of the text.

Sign up to request clarification or add additional context in comments.

3 Comments

I also want to store each data(value) in different string.
@scoop Instead of storing in different variables you should use Array of type String. If you want code I can update the answer.
no need ..I have done by using array. Thanks for reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.