protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) 
    {         long upid = Convert.ToInt64(GridView1.DataKeys[e.NewSelectedIndex].Value);//获取GridView1中选中行的主键 
       // string upid= GridView1.DataKeys[e.NewSelectedIndex].Value.ToString(); 
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString(); 
        OleDbConnection conn = new OleDbConnection(constr); 
        conn.Open(); 
        string sSql = "delete * from upload where upid='" + upid + "'"; 
        OleDbCommand cm = new OleDbCommand(sSql, conn); 
        cm.ExecuteNonQuery(); 
        conn.Close(); 
        Response.Write(" <script>alert('资源删除成功!'); </script>"); 
    } 
错误提示:  cm.ExecuteNonQuery(); 标准表达式中数据类型不匹配。 
说明:我选择GridView中的一列要删除一个记录,GridView1中的主键upid在access数据库中是表的主键(长整型:自动编号) 
请问:是否错误在 long upid = Convert.ToInt64(GridView1.DataKeys[e.NewSelectedIndex].Value); 语句?怎么解决? 
在线等大哥帮忙!感谢

解决方案 »

  1.   


    string sSql = "delete * from upload where upid=" + upid;//去掉单引号
      

  2.   

    不要被asp、VB这样的语言迷惑了,在C#中可以更简单。同意LS的。
      

  3.   

    string sSql = "delete * from upload where upid=" + upid.ToString()
      

  4.   

    从GridView中删除选中行是不?
          
                   <asp:GridView ID="GridView1" runat="server" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting"
                        Width="250px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
                        Font-Size="11px" ShowHeader="false">
                        <Columns>
                            <asp:BoundField DataField="id" SortExpression="idVisible="False">
                            </asp:BoundField>
                            <asp:BoundField DataField="filename" SortExpression="filename" />
                            <asp:CommandField ShowDeleteButton="True" />
                        </Columns>
                    </asp:GridView>
        //DataKeyNames为表标识唯一的主键,此处为id,若不想在界面显示可以隐藏
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //删除数据
             string SqlStr = "delete from upfile where id = " + GridView1.SelectedIndex;
            string ConnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnStr);
            conn.Open();
            SqlCommand comm = new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            gv_reflsh();//重新绑定
        }
        protected void gv_reflsh()
        {
            SqlDataSource1.SelectCommand = string.Format("select id,filename from upfile order by id");
            SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet;
            SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        }