Friday, February 24, 2012

How to retrieve data from a SQLDataSource control

I have made a SQLDataSource control with the select command:

SELECT COUNT(*) AS 'Antall' FROM Utgivelse WHERE (medieID = @.medieID)

I want to use the "Antall" result programmatically in C# code. I try the following statement:

IDataReader MyReader;

MyReader = CType(SqlDataSource2.Select(DataSourceSelectArguments.Empty),IDataReader);

but it doesnot work. Can somebody help me how to get the data from th control ?

Tom

What do you mean by "it does not work"? Do you get an error message? If so, what is it? Also, what database are you accessing? SQL Server? Access? Did you set the mode of the DataSource to DataReader or leave it as the default DataSet?

Have a look at this:http://www.mikesdotnetting.com/Article.aspx?ArticleID=45

Finally, are you using the result from the datasource for any other purpose? If not, you would be better using plain ADO.NET code and ExecuteScalar() for a Count result. If you are binding the result to a control, it would be simpler to read the value from the control once it has been bound.

|||

I got the answer of my problem i the URL you gave me. My working code:

DataView dvSql = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);

int result;foreach (DataRowView drvSqlin dvSql)

{

result = (int)drvSql["antall"];

if (result > 0)

Label1.Text ="The record is in use and can not be deleted";

Thank a lot for your help !!!

Tom

No comments:

Post a Comment