0

我有一个包含文本框和下拉列表的网格视图。在 gvScheduleBattingScore_RowDataBound 事件中,我绑定下拉列表没有任何问题。按钮控件位于 gridview 之外。我实际上想在buttonclickevent 上将所有文本框值和下拉选择值提交到数据库但我不知道我哪里出错了。问题是文本框不包含任何文本并且出现异常

输入字符串的格式不正确。

请帮帮我...

<asp:GridView ID="gvScheduleBattingScore" runat="server" AllowSorting="false" AutoGenerateColumns="False" AllowPaging="false"
                 GridLines="None" CellPadding="1" CssClass="GridViewStyle" ShowFooter="false" width="100%"
                 OnRowDataBound="gvScheduleBattingScore_RowDataBound">                
                    <Columns>
                        <asp:BoundField DataField="P_PlayerId" HeaderText="Player Id" HeaderStyle-Wrap="true" HeaderStyle-Width="5%" Visible="false"/>
                        <asp:BoundField DataField="PlayerName" HeaderText="Player Name" HeaderStyle-Wrap="true" HeaderStyle-Width="30%"/>
                        <asp:TemplateField HeaderText="Playing Order" HeaderStyle-Wrap="true" HeaderStyle-Width="5%">
                            <ItemTemplate>
                                <asp:TextBox ID="txtPlayingOrder" runat="server" CssClass="TinyTexBox"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Status">
                            <ItemTemplate>
                                <asp:TextBox ID="txtStatus" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Bold By">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlBoldBy" runat="server">                       </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                   </Columns>
                </asp:GridView>
</br>
<asp:Button ID="ButtonAdd" runat="server" Text="Add" CssClass="SmallButton" 
                                 ValidationGroup="Add" onclick="ButtonAdd_Click"/>

在 ButtonClick 事件上:

protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            SqlConnection dBConnection = null;
            try
            {
                int playerId;
                short plyerOrder;
                string BatsmanStatus;
                int boldBy;

                dBConnection = new SqlConnection();
                dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["CriConn"].ConnectionString;

                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand("SP_InsertScores", dBConnection);
                cmd.CommandType = CommandType.StoredProcedure;

                foreach (GridViewRow GVRow in gvScheduleBattingScore.Rows)
                {
                    string textPlayerId = GVRow.Cells[0].Text;
                    TextBox textPlyerOrder = (TextBox)GVRow.Cells[1].FindControl("txtPlayingOrder"); 
                    TextBox textBatsmanStatus = GVRow.Cells[2].FindControl("txtStatus") as TextBox;
                    DropDownList DropDownBoldBy = (DropDownList)GVRow.Cells[18].FindControl("ddlBoldBy");

playerId = Convert.ToInt32(textPlayerId );
                    if (!string.IsNullOrEmpty(textPlyerOrder.Text))
                        plyerOrder = Convert.ToInt16(textPlyerOrder.Text);
                    if (!string.IsNullOrEmpty(textBatsmanStatus.Text))
                        BatsmanStatus = textBatsmanStatus.Text;
                    if (!string.IsNullOrEmpty(DropDownBoldBy.SelectedValue) && DropDownLbwBy.SelectedValue != "Select")
                        boldBy = Convert.ToInt32(DropDownBoldBy.SelectedValue);

                        cmd.Parameters.Add("@PlayerId", SqlDbType.NVarChar).Value = playerId;
cmd.Parameters.Add("@PlayerId", SqlDbType.Int).Value = playerId;
cmd.Parameters.Add("@plyerOrder", SqlDbType.Int).Value = plyerOrder;
cmd.Parameters.Add("@BatsmanStatus", SqlDbType.NVarChar).Value = BatsmanStatus;

                    dBConnection.Open();
                    dataAdapter.InsertCommand = cmd;
                    cmd.ExecuteNonQuery();
                    dBConnection.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    // Close data reader object and database connection
                    cmd.Dispose();
                    cmd = null;
                    if (dBConnection.State == ConnectionState.Open)
                        dBConnection.Close();
                }
4

2 回答 2

1

根据与@bhoopendra.sahoo的聊天讨论,我们得出结论,这是一个绑定问题。

当触发按钮单击事件时,GridView绑定再次导致问题。

解决方法是GridView仅绑定一次并在其他事件期间限制其绑定。

于 2013-09-11T12:00:09.790 回答
0

在将文本框值转换为 int 之前添加一个空检查

if (!string.IsNullOrEmpty(textPlayerId.Text))
    playerId = Convert.ToInt32(textPlayerId);

还要对您的代码进行这些更改,以在正确的单元格中找到文本框控件

TextBox textPlyerOrder = (TextBox)GVRow.Cells[2].FindControl("txtPlayingOrder"); 
TextBox textBatsmanStatus = (TextBox)GVRow.Cells[3].FindControl("txtStatus");
于 2013-09-11T11:12:31.017 回答