473,544 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to update database without using SqlDataSource

96 New Member
If I want to access the method in which we define update query, how can I access that method on aspx page & update the data in a grid view without direct use of an query on aspx page under sql data source?
May 17 '10 #1
28 6673
Frinavale
9,735 Recognized Expert Moderator Expert
What you would have to do is implement a method that handles the GridView.RowUpd ating Event. In this method you will have to retrieve the data from the row that is updating and use this data to update your database.

I'm not sure what you're stuck on so if you provide more information I can give you more help on the topic.

-Frinny
May 17 '10 #2
AnagJohari
96 New Member
@Frinavale
sorry frinny for my titile i use......
Actually I made a data acces layer class, buiness logic layer class & presentation layer(.aspx page). Now I want to update the data in a grid view.
In most of the books generally direct update query is written on aspx page under sql data source using update commnad property & do updation in grid view.
Here is the code
Expand|Select|Wrap|Line Numbers
  1. <asp:SqlDataSource ID="datasource1" ProviderName="System.Data.SqlClient"  runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString%>" SelectCommand="Select * From ManageClient" 
  2.  UpdateCommand="Update ManageClient Set ClientName=@ClientName,Address=@Address,MobileNo=@MobileNo,Image=@Image Where ClientId=@ClientId"></asp:SqlDataSource>
  3.  
Connect it to the grid view.

Now i do not want to use update command or connection string in sql data source.
This thing I have done in data acces layer class where I make a method of an update.
I want to use this method so I can do an update in grid view directly.
May 18 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
I've never actually used the SqlDataSource before. I like to be in more control of my database connections.

Anyways, it's not that hard to do. Delete your SqlDataSource.. .you don't need it. You don't need to use a SqlDataSource at all. GridViews can bind to DataTables, DataViews, Arrays of Objects ...and more.

So, you need to populate the object that you are going to use as your data source at some point in your page life cycle. You could do this in your Page Load event if you want to.

You could cache the object that you are going to use as a data source too. This way you don't have to repopulate the data source every page postback. This saves time but it depends on how long you can tolerate "old data" being displayed.

Anyways, once you've populated your data source (or retrieved it from cache) you set the GridView.DataSo urce property to the object you are using...then call the GridView.DataBi nd method to bind the GridView to the data source. I do this in the Page PreRender event for a bunch of reasons....for example if you bind in the Page Load event every page request you will have problems with getting the values that the user entered while editing (because these values will be over written with the data that you've bound to).

In your GridViews OnRowUpdating event you will call your data access layer's methods to do the updating....

That's pretty much it.

-Frinny
May 18 '10 #4
AnagJohari
96 New Member
@Frinavale
You said that we have to bind object with grid view at page.prerender . I think if we do the same thing on page load event it work because value is set when row updating event is fired. When this event fired it saves the values at their view state & this view state is sent to the client. That is the concept of page life cycle. Am I right?
May 19 '10 #5
Frinavale
9,735 Recognized Expert Moderator Expert
Not quite right.
What happens is that the ViewState for the GridView is loaded just before the Page Load event. This means that all of the data that the user provided for the row that they are updating is loaded into the ASP.NET controls used to access the data in your C#/VB.NET code.

If you do a DataBind in the Page Load event then all of this data is lost!
Edit: if you do a DataBind in the Page Load event every page request all of your data will be lost

It's the most common problem that people have with GridViews that I've noticed.

If you don't need to get data from the user, than yes you can do the DataBind in the Page Load event. But if you need to get data from the user for updating purposes then, no, you should do it in the Page PreRender event instead.

-Frinny
May 19 '10 #6
AnagJohari
96 New Member
@Frinavale
The event is fired between load view state & save view state when the event is fired it set the values in textbox or whatever server control we used. After that it saves the values in view state. Then there is no matter of data loss if you load the page again but I have to bind data in a grid view through view state on page load no chance of data loss.
May 19 '10 #7
Frinavale
9,735 Recognized Expert Moderator Expert
You are mistaken.

Events are created when the ViewState is loaded. Methods that handle events occur after the Page Load event.

This is the basic outline of what happens in the ASP.NET Life Cycle:
  • Page Init (before ViewState is loaded)
  • Page Load (after ViewState is loaded)
  • Event handling code is executed
  • Page PreRender (occurs Just before the page is rendered as HTML)

Please review the ASP.NET Page Life Cycle for a more detailed explination.

If you perform a GridView.DataBi nd in the Page Load event (without checking if it's postback) then you will not be able to retrieve user input in the GridView.RowEdi ting event.

-Frinny
May 19 '10 #8
AnagJohari
96 New Member
@Frinavale
Can you tell me when the data is saved in a view state & that data is loss from the view state next time when it load the view state?
May 19 '10 #9
Frinavale
9,735 Recognized Expert Moderator Expert
The ViewState is stored some time in between the Page PreRender event and the SaveStateComple te event (heading towards the Render stage).

All of this is covered in the article I sent you a link to :)

I'm not sure what you mean by:
AnagJohari: that data is loss from the view state
Maybe you're talking about data loss that happens if you don't call the DataBind method??
May 19 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3808
by: Nigi | last post by:
I've made a mysql database with php front end which, in part, contains mailing lists. I've used a mailto: link to create a new email with their names in the bcc: field. Unfortunatley their is a limit on the size of a mailto: field that outlook can accept, and so it can't be used to mail more than about 80 people at a time. Our lists go up...
3
1851
by: phanimadhav | last post by:
hi this is sudheer, i am new one of this ASP.NET .i have one problem,i am using gridview control i know how to place the dropdownlistbox in gridview control.in my gridveiw control have contain totally 4 columns. The first 3 columns are bounded fields and the 4 th one is dropdownlistcontrol.the first 3 boundedfields are filled by using one table...
0
1520
by: jackvel | last post by:
Hi Dudes... I want to connect the Access database with VB 6.0 without using DataControl and ADODC.. Which option should i use.? Plz send me the detailed procedure of doing the same.. Plz help me, i need it urgently.. Thanks..
3
5347
by: mathewgk80 | last post by:
Hi all, I would like to connect gridview with database without using sql datasource.. I am using sql server and c#.net.. Please help me.. regards, Mathew.
4
7828
by: jaz215 | last post by:
hi! how do i add a record in a database without using the datacontrol and adodc. i want to rework my code to using purely codes and not being dependent on the design on vb. so far i have. Set rs = New ADODB.Recordset Set con = New ADODB.Connection con.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=C:\Program...
10
4095
by: gagonm | last post by:
Hi I m looking for some sample example (Code) for my following scenario I have a Grid View which populates data from database say from Product Tables using dataset and dataadapter This Grid has three columns of productid ,ProductName ,Unit Price (THis is editable text box field). There is checkbox column. (Paging is enabled there are...
30
4340
vikas1111
by: vikas1111 | last post by:
This is the code which i have written..... Option Explicit Dim c As New ADODB.Connection Dim cm As ADODB.Command Dim rs As New ADODB.Recordset Private Sub regno_Change() c.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/Comion.mdb;Persist Security Info=false") rs.Open "select * from main_table", c, adOpenDynamic Me.regno.Text =...
1
3169
by: agarwalsunitadhn | last post by:
I am creating gridview using connectionstring. i am passing different queries to bind grid as per condition. Now the problem I am facing on editing the grid. I am not able to use boundfield or template field because data coming from different table as per condition and number of field are also different. I can edit the data with the following...
0
1314
by: shankarnag | last post by:
i had dumped mysql database by using mysqldump.exe.If i didnt leave space between tables it is showing mysqldump.exe has stopped working.How can i overcome this problem?
0
7614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5301
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
671
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.