Friday, February 24, 2012

How to retrieve data from sqldatasource?

Okay, I used the SQLDataSource control to get my data from the databasetable. What or how do I retrieve individual data from thesqldatasource? I want to do some string comparison and manipulationbefore I display it to the browser. How can this be accomplish?

Help is appreciated.

Hi,

The SqlDataSource control designed for data bound controls so you can't access data except by data bound control, for example if your using gridview you can access data item by handle RowDataBound event and make your changes there

I hope this help.

|||

If your DataSourceMode of your SQLDataSource stays as default which is DataSet, you can retrieve a dataview object of your SQLDataSource. If DataSourceMode="DataReader" in yourSQLDataSource, you can retrieve a datareader object.

Here is an example for dataview:

'DataView
Dim mydataview As System.Data.DataView =CType(SQLDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.DataView)

For Each dr As DataRow In dv.Table.Rows
...

Next

'For a Datareader

Dim myreader as System.Data.SqlClient.SqlDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.SqlClient.SqlDataReader)

While myreader.reader

...

End While

|||Many thanks for the response. Limno, I will try that. In the mean time,if SQLDataSource is for databound only control then what do I need todo so that I can grab the individual field of my table and do datacomparison and then present it on the web?|||

Hi,

It is a way you can access your datasource programmatically.

This link will give a little more information on this topic.http://aspnet.4guysfromrolla.com/articles/022206-1.aspx

|||Okay, here's what I have.

<asp:SqlDataSource ID="sqlEnewsCate" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString%>"
SelectCommand="SELECT * FROM [enewscate] WHERE ([id_ecate] = @.id_ecate) ORDER BY [order_ecate]">
<SelectParameters>
<asp:QueryStringParameter Name="id_ecate" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlEnews" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString%>"
SelectCommand="SELECT * FROM [enews] WHERE ([idmnu_nws] = @.idmnu_nws)">
<SelectParameters>
<asp:QueryStringParameter Name="idmnu_nws" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And here is my code behind in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
string newsID;
string nwsStory ="";
newsID = Request.QueryString["id"];

if (newsID =="")
{
Response.Redirect("Default.aspx?id=3");
}

if (Request.QueryString["stry"] =="" || Request.QueryString["stry"] ==null)
{
nwsStory ="";
}
OleDbDataReader newsReader = (OleDbDataReader)sqlEnews.Select(DataSourceSelectArguments.Empty);
if (newsReader.Read())
{
if (nwsStory =="" || nwsStory ==null)
{
OleDbDataReader reader = (OleDbDataReader)sqlEnewsCate.Select(DataSourceSelectArguments.Empty);
if (reader.Read())
{
string pic = Convert.ToString(reader["sidePic_ecate"]);
if (pic =="" || pic ==null)
{
imgArticle.ImageUrl ="images/11.jpg";
}
else
{
imgArticle.ImageUrl ="images/" + pic;
}
reader.Close();
}
}
else if (nwsStory =="full")
{
string pic = Convert.ToString(newsReader["sidePic_nws"]);
if (pic =="" || pic ==null)
{
imgArticle.ImageUrl ="images/11.jpg";
}
else
{
imgArticle.ImageUrl ="images/" + pic;
}
}
newsReader.Close();
}
}

And here is the error I received:

erver Error in '/Alumni' Application.

Object reference not set to an instance of an object.

Description:Anunhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about theerror and where it originated in the code.

Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 29: }
Line 30: OleDbDataReader newsReader = (OleDbDataReader)sqlEnews.Select(DataSourceSelectArguments.Empty);
Line 31: if (newsReader.Read())
Line 32: {
Line 33: if (nwsStory == "" || nwsStory == null)


Source File: e:\wwwroot\home\mySite\enews\Default.aspx.cs Line: 31

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
enews_Default.Page_Load(Object sender, EventArgs e) in e:\wwwroot\home\mySite\enews\Default.aspx.cs:31
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061



Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42|||

If your DataSourceMode of your SQLDataSource stays as default which is DataSet, you can retrieve a dataview object of your SQLDataSource. If DataSourceMode="DataReader" in yourSQLDataSource, you can retrieve a datareader object.

You need add this to your datasource:

DataSourceMode="DataReader"

|||Thanks for the immediate response. Where in the do I put DataSourceMode="DataReader" in?|||<asp:SqlDataSource ID="sqlEnewsCate" runat="server"DataSourceMode="DataReader" .....|||Thanks! I'll give that a try.|||I still received the same error.|||

You need this in the top of your code behind page too:

using System.Data.OleDb

|||I do have that. I downloaded the source code from the link you gave me.|||

OleDbDataReader newsReader = (OleDbDataReader)sqlEnews.Select(DataSourceSelectArguments.Empty);

and this one:

OleDbDataReader newsReader = (OleDbDataReader)sqlEnewsCate.Select(DataSourceSelectArguments.Empty);

Can you see the problem?

|||Thanks for the response. If you look at my code above, you'll see thatI have both those lines in my code. Unless I did it wrong somehow.

No comments:

Post a Comment