0
votes

enter image description hereIn my gridview,I have a footer template whith an insert link button, TextBox and DropDownList.In gridview I have the edit,update and delete options and in footer template,in ddl I have values from another tables,like cities,nationality, high school, etc. When I click the button "Insert" I have this error: input string was not in a correct format . I think I have to convert the values from the ddl, because in my db I use lookup wizard. Here is my cs code:

 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>

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

And at the source error: SqlDataSource1.Insert(); Stack Trace:

[FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& 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 command, ParameterCollection parameters, 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) in c:\Users\Iulia\Desktop\LICENTA\Admitere1\Admitere1\Controale\WebUserControl1.ascx.cs:63 System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +116 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +101 System.Web.UI.WebControls.LinkButton.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

1 Answers

0
votes

I can't give you a direct solution without knowing the values of each control inside the GridView. The error is caused by one or more of the insert parameters with Type="Int32" not having an integer as the value passed by its corresponding control. An example is numarCI. Try debugging lblInsert_Click and check the values of each control that should have an integer.