?
Solved

Update multiple rows at once

Posted on 2011-02-20
8
Medium Priority
?
655 Views
Last Modified: 2012-05-11
Asp.net 4

Hi

We currently have a web page with a listview control which is bound to a linq 2 sql datasource.

On each row of the list view there are textboxes and an update button which users must click to update one row at a time.

How could we could create a page on which there was multiple rows but only one update button on the page which would process all rows at once.

I guess we would use a repeater but has anybody done this?  Is linq 2 sql appropriate or is it better to use some other way to connect to sql server?

Wing
0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34940006
Hi,

try looking at this:

http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx

its not quite what your after, but the edits are commited as you leave the cell, so NO submit button at all!

An alternate method would be to force the grid to display the data in textboxes, then parse the grid when submit is clicked to do all the updates. I prefer the editable grids, but let me know if you want me to explain further.

GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 34942091
Reason for the question is to get more work done on each round trip.
What is the alternate method you mention?
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942141
Hi,

Ok posting from my mobile so can't be detaied but in essence what you'd need to do is

On the aspx page
Edit the gridview so that each column is a textbox (off the top of my head in vwd you click on the little tag to goto design modeand you can set it there)

 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942208
Sorry, hit post without finishing!
In the code behind you'll need to parse your grid on the submit button

Something like
If gridview1.hasRows
For each row in gridview
Update table
Next
Endif
I'll be able to write out the actual code in about 4hrs if you need it

Gmm
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 2000 total points
ID: 34946697
Hi,

the linq took a bit longer than expected but here you go.

On the gridview you get it to display in Textboxes by using item templates. the linq update seems to want the old and new value so we keep this in a label that we hide.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1">
<Columns>
<asp:TemplateField HeaderText="id">
<ItemTemplate>
<asp:label ID="lbid" runat="server" Width="25px"
 text='<%# Bind("id") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Textbox ID="tbFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>'/>
<asp:label ID="hiddenFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Textbox ID="tbLastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>'/>
<asp:label ID="hiddenlastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
       ContextTypeName="updateAllDataContext" EnableUpdate="True" TableName="tests">
</asp:LinqDataSource>


in the code behind for a submit button, you need to parse the table one row at a time and update the record based on the ID


Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)
         For index As Integer = 0 To GridView1.Rows.Count - 1

            Dim keyValues As New ListDictionary()
            Dim newValues As New ListDictionary()
            Dim oldValues As New ListDictionary()
            keyValues.Add("id", Int32.Parse(CType(GridView1.Rows(index).FindControl("lbid"), Label).Text))

            oldValues.Add("firstname", CType(GridView1.Rows(index).FindControl("hiddenfirstname"), Label).Text)
            oldValues.Add("lastname", CType(GridView1.Rows(index).FindControl("hiddenlastname"), Label).Text)

            newValues.Add("firstname", CType(GridView1.Rows(index).FindControl("tbfirstname"), TextBox).Text)
            newValues.Add("lastname", CType(GridView1.Rows(index).FindControl("tblastname"), TextBox).Text)


            LinqDataSource1.Update(keyValues, newValues, oldValues)

            GridView1.DataBind()


        Next


    End Sub

Hope this helps

GMM
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34946714
hi again!

you dont need these 2 lines at all:

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)


GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 35072630
Hi GMM

Sorry been away for a couple weeks and getting back to this now.

Interesting - need to mull this one over.

Thanks for the input.

REgards

Wing
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 35073391
Hi Wing,

No probs, let me know if theres any more questions

GMM
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question