输入字符串的格式不正确

时间:2015-05-19 14:53:37

标签: asp.net gridview

enter image description here在我的gridview中,我有一个页脚模板,其中有一个插入链接按钮,TextBox和DropDownList。在gridview中我有编辑,更新和删除选项,在页脚模板中,在ddl中我有来自另一个的值表格,如城市,国籍,高中等。当我点击“插入”按钮时出现此错误:输入字符串的格式不正确。我想我必须转换ddl中的值,因为在我的db中我使用查找向导。这是我的cs代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace Admitere1.Controale
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void lbInsert_Click(Object sender, EventArgs e)
        {

            TextBox txtNume = (TextBox)GridView3.FooterRow.FindControl("txtNume");
            TextBox txtPrenume = (TextBox)GridView3.FooterRow.FindControl("txtPrenume");
            TextBox txtPTata = (TextBox)GridView3.FooterRow.FindControl("txtPTata");
            DropDownList ddlSex = (DropDownList)GridView3.FooterRow.FindControl("ddlSex");
            DropDownList ddlLN = (DropDownList)GridView3.FooterRow.FindControl("ddlLN");
            DropDownList ddlLD = (DropDownList)GridView3.FooterRow.FindControl("ddlLD");
            DropDownList ddlNationalitate = (DropDownList)GridView3.FooterRow.FindControl("ddlNationalitate");
            TextBox txtCnp = (TextBox)GridView3.FooterRow.FindControl("txtCnp");
            TextBox txtSerie = (TextBox)GridView3.FooterRow.FindControl("txtSerie");
            TextBox txtNumar = (TextBox)GridView3.FooterRow.FindControl("txtNumar");
            TextBox txtTelefon = (TextBox)GridView3.FooterRow.FindControl("txtTelefon");
            TextBox txtEmail = (TextBox)GridView3.FooterRow.FindControl("txtEmail");
            DropDownList ddlLiceu = (DropDownList)GridView3.FooterRow.FindControl("ddlLiceu");
            DropDownList ddlSala = (DropDownList)GridView3.FooterRow.FindControl("ddlSala");
            DropDownList ddlAdmis = (DropDownList)GridView3.FooterRow.FindControl("ddlAdmis");
            DropDownList ddlUser = (DropDownList)GridView3.FooterRow.FindControl("ddlUser");



            SqlDataSource1.InsertParameters["Nume"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtNume")).Text;
            SqlDataSource1.InsertParameters["Prenume"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtPrenume")).Text;
            SqlDataSource1.InsertParameters["prenumeTata"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtPTata")).Text;
            SqlDataSource1.InsertParameters["Sex"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlSex")).SelectedValue;
            SqlDataSource1.InsertParameters["LocalitateNastere"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlLN")).SelectedValue;
            SqlDataSource1.InsertParameters["LocalitateDomiciliu"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlLD")).SelectedValue;
            SqlDataSource1.InsertParameters["Nationalitate"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlNationalitate")).SelectedValue; ;
            SqlDataSource1.InsertParameters["Cnp"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtCnp")).Text;
            SqlDataSource1.InsertParameters["SerieCI"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtSerie")).Text;
            SqlDataSource1.InsertParameters["NumarCI"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtNumar")).Text;
            SqlDataSource1.InsertParameters["Telefon"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtTelefon")).Text;
            SqlDataSource1.InsertParameters["Email"].DefaultValue = ((TextBox)GridView3.FooterRow.FindControl("txtEmail")).Text;
         //   SqlDataSource1.InsertParameters["A2aFacultate"].DefaultValue = ((CheckBox)GridView3.FooterRow.FindControl("ckFac")).Text;
            SqlDataSource1.InsertParameters["Liceu"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlLiceu")).SelectedValue;
           // SqlDataSource1.InsertParameters["Inmatriculat"].DefaultValue = ((CheckBox)GridView3.FooterRow.FindControl("ckInmatriculat")).Text;
           // SqlDataSource1.InsertParameters["Inmatriculat"].DefaultValue = ((CheckBox)GridView3.FooterRow.FindControl("ckInmatriculat")).Text;
            SqlDataSource1.InsertParameters["Sala"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlSala")).SelectedValue;
            SqlDataSource1.InsertParameters["admisLa"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlAdmis")).SelectedValue;
            SqlDataSource1.InsertParameters["User"].DefaultValue = ((DropDownList)GridView3.FooterRow.FindControl("ddlUser")).SelectedValue;

            SqlDataSource1.Insert();
            this.GridView3.ShowFooter = true;
            this.GridView3.DataBind();
            Response.Redirect(Request.Url.AbsolutePath);

        }




         }
}

 And a part of my ascx code:

     <asp:GridView ID="GridView3" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="idCandidat" DataSourceID="SqlDataSource1" ShowFooter="True">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:TemplateField HeaderText="idCandidat" InsertVisible="False" SortExpression="idCandidat">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("idCandidat") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("idCandidat") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                <asp:LinkButton ValidationGroup="INSERT" OnClick="lbInsert_Click" ID="lbInsert" runat="server">Insert</asp:LinkButton>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="nume" SortExpression="nume">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("nume") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="eqNume" runat="server" ErrorMessage="Introduceţi numele"
                    ControlToValidate="TextBox1" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("nume") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID="txtNume" runat="server" style="margin-top: 15px"></asp:TextBox>
                 <asp:RequiredFieldValidator ValidationGroup="INSERT" ID="eqInsertNume" runat="server" ErrorMessage="Introduceţi numele"
                    ControlToValidate="txtNume" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
            </asp:TemplateField>
.............................................................................

    <asp:TemplateField HeaderText="user" SortExpression="user">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox16" runat="server" Text='<%# Bind("user") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="eqUser" runat="server" ErrorMessage="Introduceţi userul"
                    ControlToValidate="TextBox16" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label17" runat="server" Text='<%# Bind("user") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlUser" runat="server" DataSourceID="SqlDataSource8" DataTextField="email" DataValueField="email" Height="21px"  Width="126px" style="margin-top:15px" >
            <asp:ListItem>User</asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource8" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString9 %>" ProviderName="<%$ ConnectionStrings:ConnectionString9.ProviderName %>" SelectCommand="SELECT [email] FROM [USERI]"></asp:SqlDataSource>
                    <asp:RequiredFieldValidator ValidationGroup="INSERT" ID="eqInsertUser" runat="server" ErrorMessage="Introduceţi userul"
                    ControlToValidate="ddlUser" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    <asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red" runat="server" />
    <p>
    <asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server" />
        <p>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString9 %>" 
            DeleteCommand="DELETE FROM [CANDIDATI] WHERE [idCandidat] = ?" 
            InsertCommand="INSERT INTO [CANDIDATI] ([idCandidat], [nume], [prenume], [prenumeTata], [sex], [localitateNastere], [localitateDomiciliu], [nationalitate], [cnp], [serieCI], [numarCI], [telefon], [email], [a2aFacultate], [liceu], [înmatriculat], [sala], [admisLa], [user]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ProviderName="<%$ ConnectionStrings:ConnectionString9.ProviderName %>" 
            SelectCommand="SELECT * FROM [CANDIDATI]" 
            UpdateCommand="UPDATE [CANDIDATI] SET [nume] = ?, [prenume] = ?, [prenumeTata] = ?, [sex] = ?, [localitateNastere] = ?, [localitateDomiciliu] = ?, [nationalitate] = ?, [cnp] = ?, [serieCI] = ?, [numarCI] = ?, [telefon] = ?, [email] = ?, [a2aFacultate] = ?, [liceu] = ?, [înmatriculat] = ?, [sala] = ?, [admisLa] = ?, [user] = ? WHERE [idCandidat] = ?">
            <DeleteParameters>
                <asp:Parameter Name="idCandidat" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="idCandidat" Type="Int32" />
                <asp:Parameter Name="nume" Type="String" />
                <asp:Parameter Name="prenume" Type="String" />
                <asp:Parameter Name="prenumeTata" Type="String" />
                <asp:Parameter Name="sex" Type="String" />
                <asp:Parameter Name="localitateNastere" Type="Int32" />
                <asp:Parameter Name="localitateDomiciliu" Type="Int32" />
                <asp:Parameter Name="nationalitate" Type="Int32" />
                <asp:Parameter Name="cnp" Type="String" />
                <asp:Parameter Name="serieCI" Type="String" />
                <asp:Parameter Name="numarCI" Type="Int32" />
                <asp:Parameter Name="telefon" Type="String" />
                <asp:Parameter Name="email" Type="String" />
                <asp:Parameter Name="a2aFacultate" Type="Boolean" />
                <asp:Parameter Name="liceu" Type="Int32" />
                <asp:Parameter Name="înmatriculat" Type="Boolean" />
                <asp:Parameter Name="sala" Type="Int32" />
                <asp:Parameter Name="admisLa" Type="Int32" />
                <asp:Parameter Name="user" Type="Int32" />
            </InsertParameters>

.................................................................

并且在源错误:SqlDataSource1.Insert(); 堆栈跟踪:

  

[FormatException:输入字符串的格式不正确。]      System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer&amp; number,NumberFormatInfo info,Boolean parseDecimal)+10689507      System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info)+145      System.String.System.IConvertible.ToInt32(IFormatProvider provider)+46      System.Convert.ChangeType(Object value,TypeCode typeCode,IFormatProvider provider)+297      System.Web.UI.WebControls.Parameter.GetValue(Object value,String defaultValue,TypeCode type,Boolean convertEmptyStringToNull,Boolean ignoreNullableTypeChanges)+126      System.Web.UI.WebControls.Parameter.GetValue(Object value,Boolean ignoreNullableTypeChanges)+63      System.Web.UI.WebControls.Parameter.get_ParameterValue()+ 40      System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context,Control control)+247      System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand命令,ParameterCollection参数,IDictionary exclusionList)+257      System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values)+196      System.Web.UI.WebControls.SqlDataSource.Insert()+19      admitere1.Controale.WebUserControl1.lbInsert_Click(Object sender,EventArgs e)位于c:\ Users \ Iulia \ Desktop \ LICENTA \ Admitere1 \ Admitere1 \ Controale \ WebUserControl1.ascx.cs:63      System.Web.UI.WebControls.LinkBut​​ton.OnClick(EventArgs e)+116      System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument)+101      System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+9643314      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1724

1 个答案:

答案 0 :(得分:0)

我无法在不知道GridView内每个控件的值的情况下为您提供直接的解决方案。该错误是由一个或多个插入参数引起的,Type="Int32"没有整数作为其相应控件传递的值。一个例子是numarCI。尝试调试lblInsert_Click并检查每个应该有整数的控件的值。