Re: Input String Is Not In A Valid Format Error While Updating
I have a code for Updating the Row in a gridview, but the issue is that, when I edit and update the Row it is giving me error as Input string was not in a correct format. At the below line of Code:-
cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active);
Please see my code for your reference:-
<asp:GridView ID="grdPostData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;"
CellPadding="3" AutoGenerateColumns="False" AllowPaging="true" PageSize="4" CssClass="hoverTable"
OnPageIndexChanging="grdPostData_PageIndexChanging" OnRowEditing="grdPostData_RowEditing"
OnRowDataBound="grdPostData_RowDataBound" DataKeyNames="Id" OnRowCommand="grdPostData_RowCommand" OnRowUpdating="grdPostData_RowUpdating">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="30"
ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Post Category" ItemStyle-Width="50">
<ItemTemplate>
<asp:DropDownList ID="ddlPostCategory" AppendDataBoundItems="true" runat="server"
AutoPostBack="false">
<%-- <asp:ListItem Text="Select" Value="0"></asp:ListItem>--%>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" CommandName="eDelete" CommandArgument='<%# Bind("Id") %>' ImageUrl="~/images/delete.png"
runat="server" Width="15px" Height="15px" CausesValidation="False"
OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" CausesValidation="false"
ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png"
UpdateImageUrl="~/images/update.png">
<ControlStyle Height="20px" Width="20px"></ControlStyle>
<ItemStyle Width="15px"></ItemStyle>
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
No Result Found
</EmptyDataTemplate>
</asp:GridView>
Also see the CS code:-
protected void grdPostData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool IsUpdated = false;
//getting key value, row id
int Id = Convert.ToInt32(grdPostData.DataKeys[e.RowIndex].Value.ToString());
//get all the row field detail here by replacing id's in FindControl("")..
GridViewRow row = grdPostData.Rows[e.RowIndex];
string PostTitle = ((TextBox)(row.Cells[0].Controls[0])).Text;
string Postdesc = ((TextBox)(row.Cells[1].Controls[0])).Text;
// string ddlPostCategory = ((DropDownList)(row.Cells[2].Controls[0])).SelectedValue;
string Active = ((TextBox)(row.Cells[3].Controls[0])).Text;
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE tbl_Post SET title=@title, description=@description, active=@active WHERE Id=@Id";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 155).Value = PostTitle;
cmd.Parameters.Add("@description", SqlDbType.NVarChar, 99999999).Value = Postdesc;
cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active);
cmd.Connection = conn;
conn.Open();
IsUpdated = cmd.ExecuteNonQuery() > 0;
conn.Close();
}
if (IsUpdated)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('page updated sucessfully');window.location ='csrposts.aspx';", true);
BindGrid();
}
else
{
//Error while updating details
grdPostData.EditIndex = -1;
//bind gridview here..
//GET GDATA FROM DATABASE AND BIND TO GRID VIEW
}
}
I know this is not worth a question, tried but couldn't find. Please help.
Code for Dropdownlist aspx:-
<div class="controls">
<asp:DropDownList ID="ddlActiveInactive" CssClass="selectpicker form-control-drp wd"
runat="server" Style="width: 100%" ValidationGroup="AddNew">
<asp:ListItem Value="1" Text="Active"></asp:ListItem>
<asp:ListItem Value="0" Text="InActive"></asp:ListItem>
</asp:DropDownList>
</div>
c# asp.net gridview