hi there,
i have managed to pull in data from my sql table into my asp.net page. i am only getting one row. how can i get all rows to display that are containg in the table.
i have this so far:
myCon.Open();
string qry2 = "select client_ref, crosk_ref, client_name, section_name from md_overall_temp where client_code='" + lblRefNum.Text + "'";
SqlCommand cmd2 = new SqlCommand(qry2, myCon);
SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
adapter2.Fill(dt2);
if (dt2.Rows.Count > 0)
{
string clientRef = dt2.Rows[3]["Client_ref"].ToString();
CLIENTREF.Text = clientRef;
string croskRef = dt2.Rows[3]["Crosk_ref"].ToString();
CROSKREF.Text = croskRef;
string clientName = dt2.Rows[3]["Client_name"].ToString();
CLIENTNAME.Text = clientName;
string section_name = dt2.Rows[3]["Section_name"].ToString();
SECTIONNAME.Text = section_name;
}
else
{
CLIENTREF.Text = "No record found!";
}
myCon.Close();
Tuhin PaulPosted Mar 28, 2024, 3:32 AM
you can definitely stop the resulting output from being one lump of text and assign it to separate labels for better organization.
StringBuilderhere. We can directly concatenate strings. Create separate variables to store the formatted text for each label (clientRefText,croskRefText, etc.). This improves code readability. Inside the loop, we append the retrieved values from each row with line breaks (
) to the corresponding text variables.fergusPosted Mar 27, 2024, 3:27 PM
Thomas thank you for your solution. how can i stop the resulting output coming out in one lump of text? could i assign it to labels in any way?
fergusPosted Mar 27, 2024, 2:15 PM
thats super guys thank you!!!
Jayraj ChhayaPosted Mar 27, 2024, 1:46 PM
The issue you're encountering is that you're only accessing a single row (
dt2.Rows[3]) from the DataTabledt2, which is why you're only seeing one row displayed. It would help if you iterated through each row in the DataTable to display all rows. Here's how you can modify your code to achieve that:Jithu ThomasPosted Mar 27, 2024, 1:12 PM
It seems like you are fetching data from your SQL table and displaying it on an ASP.NET page. The issue you're facing is that you're only retrieving one row from the table and trying to access the fourth row (
dt2.Rows[3]) which may not exist. To display all rows, you need to iterate over the rows in theDataTable. Here's how you can modify your code to display all rows: -