ASP updates all records in the DATAGRID once

Source: Internet
Author: User

In asp.net, how does one update all records in the datagrid at a time? You can use the following method. First,
Create a template column for The columns to be updated in the DATAGRID, for example:


Asp: datagrid id = "dgPopularFAQs" runat = "server"
AutoGenerateColumns = "False"
...>

<Columns>
<Asp: BoundColumn DataField = "FAQID" ItemStyle-Width = "10%"
ItemStyle-HorizontalAlign = "Center" HeaderText = "faq id"/>

<Asp: BoundColumn DataField = "CategoryName" HeaderText = "Category"/>
  
<Asp: TemplateColumn HeaderText = "Question">
<ItemTemplate>
<Asp: TextBox runat = "server" id = "txtDescription" Columns = "75"
Text = <% # Container. DataItem ("Description") %>/>
</ItemTemplate>
</Asp: TemplateColumn>
  
<Asp: TemplateColumn HeaderText = "Submitted By">
<ItemTemplate>
<Asp: TextBox runat = "server" id = "txtSubmittedBy"
Text = <% # Container. DataItem ("SubmittedByName") %>/>
</ItemTemplate>
</Asp: TemplateColumn>

</Columns>
</Asp: datagrid>
Then, traverse the DATAGRID once,
Dim myConnection as New SqlConnection (connection string)
Dim myCommand as New SqlCommand (strSQL, myConnection)

Dim dgi as DataGridItem
For Each dgi in dgPopularFAQs. Items
Read in the Primary Key Field
Dim id as Integer = Convert. ToInt32 (dgPopularFAQs. DataKeys (dgi. ItemIndex ))
Dim question as String = CType (dgi. FindControl ("txtDescription"), TextBox). Text
Dim submittedBy as String = CType (dgi. FindControl ("txtSubmittedBy"), TextBox). Text
  
Issue an UPDATE statement...
Dim updateSQL as String = "UPDATE TableName SET Question = @ Question ,"&_
"SubmittedByName = @ SubmittedByName where faqid = @ ID"
MyCommand. Parameters. Clear ()
MyCommand. Parameters. Add ("@ Question", question)
MyCommand. Parameters. Add ("@ SubmittedByName", submittedBy)
  
MyCommand. ExecuteNonQuery ()
Next


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.