如何将每个文本框值更新为gridview中的每个行列,然后将值更新到数据库中

时间:2015-05-28 06:47:31

标签: asp.net vb.net gridview

我在编码和寻求每个人的帮助时遇到了一些困难。 我在gridview中添加了文本框,并在gridview中绑定了数据库详细信息。 文本框的目的是更新名为“Total Redeemed Total”的列中的值。 我编写了以下代码,但问题出现了。 只有最后一行文本框值才会更新所有列值。 例如,我的最后一行文本框值为'2',数据库会将“Total Redeemed Total”的所有列更新为2.这可能是因为我的for循环? 或者我应该使用rowdatabound? 希望有人可以清除我的疑问,谢谢。

我的HTML代码(Gridview)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px" 
        CellPadding="3" GridLines="Vertical" HeaderStyle-Height="30px" style="border-left-color: black; border-bottom-color: black; border-top-style: dashed; border-top-color: black; border-right-style: dashed; border-left-style: dashed; background-color: silver; border-right-color: black; border-bottom-style: dashed" BackColor="White" BorderColor="#999999" BorderStyle="None">
        <Columns>
        <asp:TemplateField  HeaderText="Transaction No" >
                <HeaderStyle HorizontalAlign="Left" Width="70px"/>
                <ItemTemplate>
                    <asp:Label ID="lblTransactionNo" runat="server" text='<%# Eval("TransactionNo") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="PaymentDateTime" HeaderText="Payment Date Time">
                <HeaderStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="ItemCode" HeaderText="Ticket Code"  >
                <HeaderStyle HorizontalAlign="Left" Width="65px"/>
            </asp:BoundField>
            <asp:BoundField DataField="programdesc" HeaderText="Program Description">
                <HeaderStyle HorizontalAlign="Left" Width="95px" />
            </asp:BoundField>

            <asp:TemplateField  HeaderText="Quantity Available">
                <HeaderStyle HorizontalAlign="Left" Width="70px"/>
                <ItemTemplate>
                    <asp:Label ID="lblAvailable" runat="server" text='<%# Eval("Quantity") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField  HeaderText="Total Quantity Redeemed">
                <HeaderStyle HorizontalAlign="Left" Width="70px"/>
                <ItemTemplate>
                    <asp:Label ID="lblRedeemedQty" runat="server" text='<%# Eval("RedeemedQuantity") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Quantity to Redeem" ShowHeader="False">
            <HeaderStyle HorizontalAlign="Left" Width="95px" />
                <ItemTemplate>
                    <asp:TextBox ID="txtRedeem" runat="server" Text="0"></asp:TextBox>
                     <asp:CompareValidator Display="Dynamic" ID="CompareValidator1" Operator="DataTypeCheck" Type="Integer"  runat="server" ErrorMessage="Only Integer!" ControlToValidate="txtRedeem"></asp:CompareValidator>
                     <asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator1" runat="server" ErrorMessage="Key In Value!" ControlToValidate="txtRedeem"></asp:RequiredFieldValidator>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
        </Columns>

        <HeaderStyle CssClass="gridheader" Height="30px" BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle CssClass="gridaltrow" BackColor="#DCDCDC" />
        <RowStyle CssClass="gridrow" BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    </asp:GridView>

我背后的代码

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim sqlQuery As String
    Dim conn As SqlConnection = New SqlConnection("Server=gsc-volvo-Hp;Database=GET_PANORAMA;Trusted_Connection=True;")
    Dim cmd As SqlCommand
    'Dim dr As SqlDataReader
    Dim da As SqlDataAdapter = New SqlDataAdapter()


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Page.IsPostBack = False) Then

            Dim cmd As SqlCommand
            'Dim dr As SqlDataReader
            Dim da As SqlDataAdapter = New SqlDataAdapter()

            conn.Open()
            sqlQuery = "SELECT webtpay_trn.*,PaymentDateTime from webtrcp_trn inner join webtpay_trn on (webtrcp_trn.TransactionNo = webtpay_trn.TransactionNo)"
            cmd = New SqlCommand(sqlQuery, conn)
            da.SelectCommand = New SqlCommand(sqlQuery, conn)

            GridView1.DataSource = cmd.ExecuteReader()
            GridView1.DataBind()
            conn.Close()
        End If

    End Sub




Private Sub CheckQty()
Dim row As GridViewRow
Dim total As Integer
lblMessage.Text = ""

For Each row In GridView1.Rows
    Dim lblAvailable As Integer
    Dim lblRedeemedQty As Integer
    Dim amtRedeem As Integer
    Dim transNo As String

    ' write ur DB process code here

    row.ForeColor = Drawing.Color.Empty
    lblMessage.ForeColor = Drawing.Color.Empty

    Try
        lblAvailable = CType(row.FindControl("lblAvailable"), Label).Text
        amtRedeem = CType(row.FindControl("txtRedeem"), TextBox).Text
        lblRedeemedQty = CType(row.FindControl("lblRedeemedQty"), Label).Text
        transNo = CType(row.FindControl("lblTransactionNo"), Label).Text

    Catch ex As Exception
        lblError.ForeColor = System.Drawing.Color.Red
        lblError.Text = "** Error Occur In Quantity Redeem.Default=0 **"
    End Try

    total = total + amtRedeem

    If (amtRedeem > lblAvailable) Or amtRedeem < 0 Then
        lblMessage.ForeColor = System.Drawing.Color.Red
        lblMessage.Text = "** Quantity Error or Insufficient. Please Check Your Redeem Quantity **"

        row.ForeColor = Drawing.Color.Red
        row.Focus()

        Exit For

    ElseIf total = 0 Then
        lblMessage.ForeColor = System.Drawing.Color.Red
        lblMessage.Text = "** No Amount Entered. Please Enter Some Value **"

    Else


    sqlQuery = sqlQuery + "Update WebTpay_Trn set RedeemedQuantity =RedeemedQuantity + '" & amtRedeem & "' "
    sqlQuery = sqlQuery + " WHERE webtpay_trn.TransactionNo = '" & transNo & "'  "
        Dim cmd As SqlCommand = New SqlCommand(sqlQuery, conn)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

        'lblMessage.ForeColor = System.Drawing.Color.Green
        lblMessage.Text = " Total Redeem =  " + total.ToString + " Tickets "
        'Response.Redirect("showTicketRedeem.aspx")
    End If

Next
End Sub


    Protected Sub btnRedeem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRedeem.Click

        CheckQty()

    End Sub

End Class

3 个答案:

答案 0 :(得分:0)

好吧我想问题是你已经声明了变量amtRedeem and total outside the loop所以这些变量在下一次迭代中保持old values。所以你需要声明这些inside the loop。我不确定变量totalamtRedeem必须在循环内。

For Each row In GridView1.Rows
Dim amtRedeem As Integer
Dim total As Integer
//Rest of your code will remain same.

答案 1 :(得分:0)

认为它可能是SQL更新语句

        series: [{
            data: [
            [0, 291],
            [1, 289],
            [2, 289],
            [3, 285],
            [4, 284],
            [5, 283],
            [6, 263]
        ],
        color: '#FF0000'
    }, {

        data: [
            [7, 135],
            [8, 119],
            [9, 100],
            [10, 96],
            [11, 89],
            [12, 80],
            [13, 65],
            [14, 60],
            [15, 57],
            [16, 46],
            [17, 20]
        ],
        color: '#1aadce'
    }]

没有WHERE语句,因此每个RedeemedQuantity字段都使用当前行的txtRedeem文本框的值进行更新。

添加项目模板以保存每一行的TransactionNo,就像您为RedeemedQuantity所做的那样

 UPDATE WebTpay_Trn set RedeemedQuantity = '" & amtRedeem & "' 
 FROM webtrcp_trn INNER JOIN webtpay_trn ON 
 (webtrcp_trn.TransactionNo = webtpay_trn.TransactionNo)

添加一个变量来保存该行的Transaction No(例如使用名为lblTransactionNo的标签)

 <asp:TemplateField  HeaderText="TransactionNo">
            <ItemTemplate>
             <asp:Label ID="lblTransactionNo" runat="server" text='<%# Eval("TransactionNo") %>'>
             </asp:Label>
           </ItemTemplate>
       </asp:TemplateField>

您需要添加引用该行的TransactionNo

的WHERE语句

这样的东西
Dim transactionNo as string = lblTransactionNo.Text

答案 2 :(得分:0)

我不确定它是否是解决方案,但我的价值可以得到并将每个文本框插入每个特定行而没有问题,感谢Peter Campbell和Mairaj Ahmad的帮助。

按我的指示执行以下操作: Dim lblTransactionNo As Label = DirectCast(row.FindControl(“lblTransactionNo”),Label)

sqlQuery = "Update WebTpay_Trn set RedeemedQuantity = RedeemedQuantity + '" & amtRedeem & "' "
                    sqlQuery = sqlQuery + " WHERE TransactionNo = '" & lblTransactionNo.Text & "' "
                    'sqlQuery = String.Format("UPDATE WebTpay_Trn set RedeemededQuantity = {0} WHERE webtPay_trn.TransactionNo = {1}", amtRedeem, lbltransactionNo)

                    Dim cmd As SqlCommand = New SqlCommand(sqlQuery, conn)

                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()