Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I'm trying to insert ,update,edit and delete records in my grid,records are inserted successfully,but on update and delete it is giving an exception..
C#
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
       int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
       string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
       con.Open();
       SqlCommand cmd = new SqlCommand("delete from Trn_Contact1 where UserId=" + UserId, con);
       int result = cmd.ExecuteNonQuery();
       con.Close();
       if (result == 1)
       {
       BindEmployeeDetails();

       lblresult.Text = username + " details deleted successfully";
       }
       }


the exception thrown is in .. int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString()); and the exception is ... Input string was not in a correct format.
can someone please help.
Posted
Comments
Volynsky Alex 11-Aug-12 8:45am    
Interesting question

See your source file and check parameter name and that data type:

XML
<asp:SqlDataSource ID="sqlds" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="Select * from Employee_Details"
InsertCommand="insert into Employee_Details(UserName,FirstName,LastName,City,Designation) values(@UserName,@FirstName,@LastName,@City,@Designation)"
DeleteCommand="delete from Employee_Details where UserId=@UserId"
UpdateCommand="update Employee_Details set FirstName=@FirstName,LastName=@LastName, City=@City,Designation=@Designation where UserId=@UserId">
<UpdateParameters>
<asp:Parameter Name="UserId" Type= "Int32" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Designation" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Designation" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>



regards

sarva
 
Share this answer
 
v2
Try this

ASP.NET
 <asp:gridview id="gvDetails" runat="server" cssclass="Gridviews" autogeneratecolumns="false" datakeynames="empid" onrowcommand="gvDetails_RowCommand" xmlns:asp="#unknown">
<columns>
 <asp:templatefield>
 <itemtemplate>
 <asp:imagebutton id="imgdelete" runat="server" commandname="cmdDelete" commandargument='<%# Eval("empid") %>' imageurl="~/Images/delete.png">
 ToolTip="Delete" Height="20px" Width="20px" />
</asp:imagebutton></itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
 <asp:label id="lblresult" runat="server" xmlns:asp="#unknown" />

Write the following code on:

C#
 protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName == "cmdDelete")
        {
            int empid1 = Convert.ToInt32(e.CommandArgument.ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from empdetails where empid=@empid", con);
            cmd.Parameters.Add(new SqlParameter("@empid", (object)empid1));
            int res = cmd.ExecuteNonQuery();
            con.Close();
            if (res == 1)
            {
                 BindEmployeeDetails();

                lblresult.Text = "Records deleted sucessfully";
            }
        }
}
 
Share this answer
 
v3
Comments
_Amy 12-Aug-12 1:37am    
Good effort, but one error:
commandargument="<%# Eval("empid") %>"

It should be like:
commandargument='<%# Eval("empid") %>'

I Modified it.
My +4 for your effort.
_Raj sinha 12-Aug-12 2:32am    
Thanks Amit, It was a silly mistake.I'll take care of that in future.
pree_kh 14-Aug-12 0:55am    
Thanks Raj,it was of great help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900