Accessing value from SqlDataSource

M

Mike

I have a SqlDataSource that returns a list of companies and their details by
ProductID from a stored procedure. It also returns the name of the product
associated with the ProductID as the final column (which means it appears
for every record returned). I already have a way of determining how many
rows were returned, and use that information in a label to say "Your search
has returned x records" at the top of the page, above the gridview that
displays the company records:

protected void dsGetSuppliersByProduct_Selected(object sender,
SqlDataSourceStatusEventArgs e)
{
int RecordCount = e.AffectedRows;
if (RecordCount == 0)
{ lblRecordCount.Text = "<p>No Records found</p>"; }
else
{
if (RecordCount == 1)
{ lblRecordCount.Text = "<p>Your search returned 1
record</p>"; }
else
{ lblRecordCount.Text = "<p>Your search returned " + RecordCount
+ " records</p>"; }
}

}

How can I access the ProductName value so that I can extend the label text
to say "Your search has returned x records for <ProductName>" ?

Thanks

Mike
 
M

Mike

Thanks Eliyahu,

I wasn't binding the value to a control, which is why I couldn't figure out
how to access it. The value was being returned as part of the dataset
because I didn't want to query the database twice if I could help it. It's
a shame my extensive googling didn't turn your article up yesterday. As a
matter of fact, while I was driving home last night, I came up with a very
similar method and applied it successfully this morning:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string pName = DataBinder.Eval(e.Row.DataItem,
"ProductName").ToString();
if (ltProductName.Text == "")
{
ltProductName.Text = " of <strong>" + pName +
"</strong></p>";
}
}
}

ltProductName is a Literal Control, placed after another literal control
which shows the total records based on e.AffectedRows.

Thanks
Mike


Eliyahu Goldin said:
You should be able to get the value from the control bound to the
datasource. If you want to get the value from tha dataset, this article
may help you:

http://msmvps.com/blogs/egoldin/archive/2006/12/27/how-to-get-datatable-out-of-sqldatasource.aspx

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I have a SqlDataSource that returns a list of companies and their details
by ProductID from a stored procedure. It also returns the name of the
product associated with the ProductID as the final column (which means it
appears for every record returned). I already have a way of determining
how many rows were returned, and use that information in a label to say
"Your search has returned x records" at the top of the page, above the
gridview that displays the company records:

protected void dsGetSuppliersByProduct_Selected(object sender,
SqlDataSourceStatusEventArgs e)
{
int RecordCount = e.AffectedRows;
if (RecordCount == 0)
{ lblRecordCount.Text = "<p>No Records found</p>"; }
else
{
if (RecordCount == 1)
{ lblRecordCount.Text = "<p>Your search returned 1
record</p>"; }
else
{ lblRecordCount.Text = "<p>Your search returned " +
RecordCount + " records</p>"; }
}

}

How can I access the ProductName value so that I can extend the label
text to say "Your search has returned x records for <ProductName>" ?

Thanks

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top