在Asp.Net中使用GridView [英] Using GridView in Asp.Net

查看:83
本文介绍了在Asp.Net中使用GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在尝试在网格中插入,更新,编辑和删除记录,记录已成功插入,但是在更新和删除它时会出现异常..

 受保护的 无效 gvDetails_RowDeleting(对象发​​件人,GridViewDeleteEventArgs e)
       {
        int  UserId = Convert.ToInt32(gvDetails.DataKeys [e.RowIndex] .Values ["  UserId"].ToString());
       字符串用户名= gvDetails.DataKeys [e.RowIndex] .Values [" ].ToString();
       con.Open();
       SqlCommand cmd =  SqlCommand("  + UserId,同);
        int  result = cmd.ExecuteNonQuery();
       con.Close();
       如果(结果==  1 )
       {
       BindEmployeeDetails();

       lblresult.Text =用户名+ " ;
       }
       } 



引发的异常位于.. int UserId = Convert.ToInt32(gvDetails.DataKeys [e.RowIndex] .Values ["UserId"].ToString());唯一的例外是...输入字符串的格式不正确.
有人可以帮忙吗?

解决方案

查看您的源文件并检查参数名称和该数据类型:

 <   asp:SqlDataSource     ID   ="     runat   =" 服务器"  ConnectionString    <% 


ConnectionStrings:dbconnection %> " SelectCommand =" InsertCommand =" DeleteCommand =" UpdateCommand =" < UpdateParameters > < asp:Parameter 名称 =" 类型 Int32 " / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < /UpdateParameters > < InsertParameters > < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < /InsertParameters > < /asp:SqlDataSource > < /div >




问候

sarva


尝试一下

 <   asp:gridview     id   ="   ="span>    runat   =" 服务器"  cssclass    Gridviews"  autogeneratecolumns   ="    数据关键字名  ="     onrowcommand   ="     ="  #unknown" <   > 
 <   asp:templatefield  > 
 <   itemtemplate  > 
 <   asp:imagebutton     id   ="   runat   服务器" 命令名   cmdDelete" 命令参数  " )%>'    ="   <  /asp:imagebutton  >  <  /itemtemplate  > 
<  /asp:templatefield  > 
<  /列 > 
<  /asp:gridview  > 
 <   asp:label     id   ="   runat   服务器"  xmlns:asp   #unknown" / >  


在以下代码上编写以下代码:

 受保护的  void  gvDetails_RowCommand(对象发​​件人,GridViewCommandEventArgs e)
    {
如果(例如CommandName == " 跨度>)
        {
             int  empid1 = Convert.ToInt32(e.CommandArgument.ToString());
            con.Open();
            SqlCommand cmd =  SqlCommand(" ,骗子);
            cmd.Parameters.Add( SqlParameter(" ,( int  res = cmd.ExecuteNonQuery();
            con.Close();
            如果(分辨率==  1 )
            {
                 BindEmployeeDetails();

                lblresult.Text = " ;
            }
        }
} 


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..

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.

解决方案

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

<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


Try this

 <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:

 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";
            }
        }
}


这篇关于在Asp.Net中使用GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆