473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is possible to edit the table returned by SqlDataReader?

Hi all,

I execute a stored procedure in my C# code, assign the result to the
SqlDataReader object and display it with datagrid.

the question I like to ask, is possible to edit the datagrid output
(eg. delete a row in the datagrid) and update the change in the
corresponding table in the database?

thanks for your time.

Wing

Nov 17 '05 #1
5 12104
No you cannot edit table returned by SQL reader you must have dataset as
datasource and then create adapter and call its Update method with dataset of
datagrid.

Hope this may help you...

"Wing" wrote:
Hi all,

I execute a stored procedure in my C# code, assign the result to the
SqlDataReader object and display it with datagrid.

the question I like to ask, is possible to edit the datagrid output
(eg. delete a row in the datagrid) and update the change in the
corresponding table in the database?

thanks for your time.

Wing

Nov 17 '05 #2
For the most part no. A data reader is by definition a read-only, forward
view of the DB contents. You can't change the values it returns. However
you can enumerate the results of the data reader and build a new data source
(such as an array of custom objects) that you could then filter and bind to
the DataGrid.

Most people just find it easier to use a DataSet when supporting editable
DataGrids. That is the textbook answer to your common problem. That is one
of the reasons why DataSets were created to begin with. I'd recommend that
approach. You can then filter and sort your results as needed prior to
sending to the DataGrid.

FYI, there are 2 DataGrids; one in ASP.NET and one in WinForms. They are
quite different. In future posts specify which one you are talking about.
You should also post such questions in the WindowsForms or ASP.NET newsgroups
instead of this language specific group. The control works the same
irrelevant of the underlying language. In this group you will only get
answers from C# people.

Hope this helps,
Michael Taylor, MCP, MCAD - 6/29/05

"Wing" wrote:
Hi all,

I execute a stored procedure in my C# code, assign the result to the
SqlDataReader object and display it with datagrid.

the question I like to ask, is possible to edit the datagrid output
(eg. delete a row in the datagrid) and update the change in the
corresponding table in the database?

thanks for your time.

Wing

Nov 17 '05 #3
"Wing" <li******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
the question I like to ask, is possible to edit the datagrid output
(eg. delete a row in the datagrid) and update the change in the
corresponding table in the database?


Yes of course, but not through the underlying SqlDataReader object itself
because that is read-only.

You would need to include a button (or somesuch) in one of the columns of
the datagrid which would cause the page to postback, where you would delete
the record, requery the data and repopulate the datagrid. Not particularly
efficient, but perfectly feasible...
Nov 17 '05 #4
Hi,

Well a datareader is a readonly , forward only server side cursor, so the
short answer is no.

you should close the reader as soon as you finish to display it anyway,
otherwise the connection will be kept open and busy.
It goes like this:
SqlDataReader reader = GetData();
grid.DataSource = reader;
grid.DataBind();
reader.Close();

What you can do is this, if the set returned has a PK ( it does right? ) you
can bind it to a non-visible column or to a hidden field otherwise.
so when you select the delete action you can get this value back, pass it as
a parameter to a SP and then rerun the above piece of code.

Let me know if you need a complete code for all the operations involved.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Wing" <li******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi all,

I execute a stored procedure in my C# code, assign the result to the
SqlDataReader object and display it with datagrid.

the question I like to ask, is possible to edit the datagrid output
(eg. delete a row in the datagrid) and update the change in the
corresponding table in the database?

thanks for your time.

Wing

Nov 17 '05 #5
thanks for you guy advice. I had use dataset before, but my dataset
contained 2 tables which have relationship
between them, and I want to display the columns selectively from these
2 tables in the same datagrid by using the
databind function. However, the datagrid only display the first table
that filled into it, that make me try the data reader.

now I change my code to using the dataset again. the solution is join 2
tables first before calling the fill
function.

the code is showing below

DataSet shoppingCartDataSet = new DataSet();

SqlDataAdapter tableJoinAdapter = new SqlDataAdapter ("SELECT
ShoppingCart.RecordID,T_Photo.PhotoName, ShoppingCart.Quantity,
T_Photo.Price, ShoppingCart.CartID FROM ShoppingCart INNER JOIN T_Photo
ON ShoppingCart.PhotoID = T_Photo.PhotoID WHERE ShoppingCart.CartID =
'"+found+"'", thisConnection);

tableJoinAdapter.Fill(shoppingCartDataSet);

DataGrid1.DataSource = shoppingCartDataSet;
DataGrid1.DataBind();

Holpfully, I can update any changes to my database tables more easily.
If anyone find out any mistake in this code, please let me know.

thanks you very much.

wing

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: po or | last post by:
Enterprise Manager wants to dial in to the Internet when I try to open up table design, and if I cancel the dial-in, the MMC window is simply blank -- no table design editing. This is rather...
1
by: paul goldney | last post by:
I have 1 table in a 200+ table database The database is Merge Synchornised and has been working fine for 2 years + The same database is at several customers and the DB is fully relational I...
2
by: News East | last post by:
I have an existing SQL 7 server named HHARBR. HHARBR has a database named SPR with a table named "reportname" in it, the table has the name HHARBR embedded the table data. I migrated the HHARBR...
1
by: gjtired | last post by:
Hi I am using a table to fill a dropdown box. The contents of the dropdown box are ordered by a seq_no. I edit the seq_no and want to refresh the dropdown box and show the new order. I can edit...
1
by: Stephen Lynch | last post by:
What is the best way to insure that an edit table fields are saved on an exit. For example, I have an edit panel on a page. I do not want to post back on every field, but if an end user goes to...
11
by: Sheau Wei | last post by:
i wish to know how to create a dinamically edit table entries .can u help me ?thanks.
0
by: =?Utf-8?B?QWRhbSBDYXJsc29u?= | last post by:
I have a database with one table, and a button on the webpage. I want to run some math on a value stored in that table (column1, row1 -- for example) when the user clicks the Button. i DONT want...
4
dreamfalcon
by: dreamfalcon | last post by:
Hi! I'm looking for a method to edit table cells. Something similar to Google Docs, that when the user double clicks the cell, it replaces the content with a textbox that grows automatically, and...
3
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.