Black Hat
Black Hat

Reputation: 35

How to read data from two different column in same table and return in string builder?

im having a problem in reading data from two different column in same database table. (Sorry for my bad english.)

String status = "1";

SqlConnection conn = new SqlConnection("Server=localhost;Integrated Security=true;database=WebsiteDatabase");
conn.Open();
SqlCommand cmdname = conn.CreateCommand();
cmdname.CommandText = "select product, quantity from tlbOrder where status = '1'";

//get product name and quantity
cmdname.Parameters.Add("@status", status);
SqlDataReader dr = cmdname.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (dr.Read())
{
    sb.AppendFormat("{0}, ", dr[0].ToString());
}


label1 = ""+sb.ToString();

However, only "product" details are return.

Expected results: label with string of Product name + quantity (e.g. Laptop 1, Keyboard 3, Speaker 5) and so on.

Upvotes: 2

Views: 247

Answers (1)

Steve
Steve

Reputation: 216313

Well, there is no problem to add another parameter to AppendFormat

while (dremail.Read())
{
    sb.AppendFormat("{0}, {1}", dr[0].ToString(), dr[1].ToString());
}

However, your code will try to insert in the same label a potentially long list of products.
Perhaps a better approach should be to use a List or a GridView

Upvotes: 5

Related Questions