循环通过gridview获取错误

时间:2012-05-25 12:50:36

标签: sql asp.net gridview

我有一个gridview,我试图循环以获取更新和存储在我的数据库中的值。 Gridview代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" 
                Width="1020px" EnableTheming="True" PageSize="25" CellPadding="4" 
                ForeColor="#333333" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="Column1" HeaderText="Col1" ReadOnly="True" >
                    <ItemStyle Width="35px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Column2" HeaderText="Col2" />
                    <asp:BoundField DataField="Column3" HeaderText="Col3" />
                    <asp:BoundField DataField="Column4" HeaderText="Col4" />
                    <asp:BoundField DataField="Column5" HeaderText="Col5" />
                    <asp:TemplateField HeaderText="Col6">
                        <EditItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column6") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text="$"></asp:Label>&nbsp;
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column6", "{0:f}") %>' CssClass="boxright"></asp:TextBox>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="Column7" ReadOnly="True" >
                    <ItemStyle Width="35px" />
                    </asp:BoundField>
                    </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
                <PagerSettings Mode="NextPreviousFirstLast" />
                <PagerStyle BackColor="#003399" ForeColor="White" HorizontalAlign="Left" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

我正在使用按钮单击事件触发存储的sql过程来更新我的表,如下所示......

Protected Sub btnB_Save_Click(sender As Object, e As EventArgs) Handles btnB_Save.Click
Dim gvRow As GridViewRow
    Dim str As String = ""
    For Each gvRow As GridViewRow In gv_Budgets.Rows
        str = ((str + gvRow.Cells(0).Text) + (gvRow.Cells(1).Text) + (gvRow.Cells(2).Text) + (gvRow.Cells(3).Text) + (gvRow.Cells(4).Text) + (gvRow.Cells(6).Text))
    Dim dtRow As TextBox = CType(gvRow.FindControl("TextBox1"), TextBox)
    Response.Write(dtRow.Text)

    dt = dal.ExecuteStoredProc(DAL.dbType.SqlServer, "UpdateData", "@col1", str + gvRow.Cells(0).Text, "@col2", str + gvRow.Cells(1).Text, "@col3", str + gvRow.Cells(2).Text, "@col4", str + gvRow.Cells(3).Text, "@col5", str + gvRow.Cells(4).Text, "@col6", dtRow.Text, "@col7", str + gvRow.Cells(6).Text, "@txt1", Textbox2.Text, "@txt2", Textbox3.Text)

    Next
End Sub 

但是,我一直在我的“dt(datatable)”表达式下得到那些蓝色语法错误行...关于这样做或为什么这样做的任何想法?

1 个答案:

答案 0 :(得分:0)

“变量gvRow隐藏......”错误正在发生,因为你已经在循环外声明了gvRow,然后在循环的定义中重新声明了它。

Dim gvRow As GridViewRow    ' First declaration
Dim str As String = ""
For Each gvRow As GridViewRow In gv_Budgets.Rows    ' Skip the "As GridViewRow here

另一个错误来自你的表达:

dt = dal.ExecuteStoredProc( ...

我假设(因为你没有列出它)dt是某个类型DataTable的全局变量。在这种情况下,看起来您的sProc返回一个字符串并尝试将其插入到数据表对象中。确保您的sProc返回有效的数据表。在此期间,您可能会尝试将结果插入到字符串中,只是为了让事情正常工作,直到您可以解决这个问题。

相关问题