Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Sunday, March 25, 2012

Change dynamically(via code) the SqlDataSource for a GridView....

Hi,

say I have two Sqldatasources objects:SqlDataSource1 and SqlDataSource2...

Does anybody know how can I alter programmatically these two sqldatasources in a gridview?

Thanks!!!

Ok,I resolved it,

I had to delete the "DataSourceID="SqlDataSource1" from the gridview directive and add the following code to a button event:

GridView1.DataSource = SqlDataSource1

GridView1.DataBind()

|||

But now my new question is :Confused

How can I see what is the current/previous DataSource of my GridView Control?

Thanks...

|||

GridView1.DataSource will return the data source (as an object)

You can also useGridView1.DataSourceID to obtain the ID of the datasource as a string

For example:

dim TheCurrentDataSource as object

TheCurrentDataSource =GridView1.DataSource

GridView1.DataSource = The NextDataSource

GridView1.DataBind()

HTH

Saturday, February 25, 2012

Chaching Sqldatasource datasets... can it be done? and a performance question.

I'm using visual basic coded datasets right now, and caching them, so as all of my web app users access the cache once the first person's gone to my site and initiallized the page.

If Cache("ds_cache")IsNothingThenDim dsAsNew DataSet

blah blah code blah blah blah

Cache.Insert("ds_cache", ds)EndIf

What I was wondering is, if it's possible to cache the datasets that get created and are used by Sqldatasources. Or if by chance caching is automatically done and controlled for these datasets, as it's the server that makes and handles them.

The reason I wonder about this because I'm not sure how you go about referencing one of those datasets, because you can't give them a name/id. And I've not found mention anywhere of you being able to do so.

The real point or question I'm trying to get at is to see which performs better , or uses less requests back to the server for the data as a lot of my data will be repeatedly used by all of my users and so I'd like to save myself from using as many repeated connections as possibles in regards to using a typed dataset versus filling my controls from a sqldatasource?

Thank you in advance for your response.

You know what, I pretty much found my answer:

http://msdn2.microsoft.com/en-us/library/z56y8ksb.aspx

The question still remains of if a sqldatasource would outperform a typed dataset?

It makes you wonder, as a typed dataset you don't have to worry about time spent on conversions, etc. but you would think that server built and controlled dataset might perform better, or atleast it would probably build faster when it's first created... please do reply if you have insight.

Chaching Sqldatasource datasets... can it be done? and a performance question.

I'm using visual basic coded datasets right now, and caching them, so as all of my web app users access the cache once the first person's gone to my site and initiallized the page.

If Cache("ds_cache") Is Nothing Then

Dim ds As New DataSet

blah blah code blah blah blah

Cache.Insert("ds_cache", ds)

End If

What I was wondering is, if it's possible to cache the datasets that get created and are used by Sqldatasources. Or if by chance caching is automatically done and controlled for these datasets, as it's the server that makes and handles them.

The reason I wonder about this because I'm not sure how you go about referencing one of those datasets, because you can't give them a name/id. And I've not found mention anywhere of you being able to do so.

The real point or question I'm trying to get at is to see which performs better , or uses less requests back to the server for the data as a lot of my data will be repeatedly used by all of my users and so I'd like to save myself from using as many repeated connections as possibles in regards to using a typed dataset versus filling my controls from a sqldatasource?

Thank you in advance for your response.

You know what, I pretty much found my answer:

http://msdn2.microsoft.com/en-us/library/z56y8ksb.aspx

The question still remains of if a sqldatasource would outperform a typed dataset?

It makes you wonder, as a typed dataset you don't have to worry about time spent on conversions, etc. but you would think that server built and controlled dataset might perform better, or atleast it would probably build faster when it's first created... please do reply if you have insight.

Thursday, February 16, 2012

Catching errors and row cnt from SQLdataSource

I'm new to using SQL Data Source, so bare with me on the newbie question.

Is there a way to do a Try...Catch type scenario on the SDS? I have a grid and a SDS that is mapped together but previously I use to use a Try...Catch and show any errors. What can I do to display a message if there is an error with the SDS?

Try
'Call to DB

Catch
label1.txt = "Error: " & ex.Message.ToString

End Try

And is the best way to determine if there are any records to display is to use the SDS_Selected event?

Dim Rec as Integer = e.AffectedRows
If Rec = 0 Then
label1.text = "No Records Found."
End If


To catch errors on a SQLDataSource I use the 'ed' events (ie Selected, Inserted, Deleted and Updated), and check that e.Exception is not null. You can returne.ExceptionHandled = True once you have handled the exception.

e.AffectedRows looks like it would be the best way to count the number of affected records (I can't say I've ever tried to catch that, so there may be other ways I don't know about)

HTH

|||

Hi,
I agree with drktrnq. Below there is a code snippet. I hope it helps you.

1Protected Sub SqlDataSource1_Selected(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)Handles SqlDataSource1.Selected2If (e.Exception IsNotNothing)Then3 Me.Label1.Text = e.Exception.Message4 e.ExceptionHandled =True5 Return6 End If78 If (e.AffectedRows = 0)Then9 Me.Label1.Text ="No records found"10Else11 Me.Label1.Text = e.AffectedRows.ToString()12End If13 End Sub
Luis Ramirez.
www.sqlnetframework.com
The SQL framework for .NET.