最近在做网页的时候发现个问题,整了好久都不能解决!GridView的Datasource是手动指向SqlDatasource的<html>中的SqlDatasource
如下<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:123099DBConnectionString %>"
         
        InsertCommand="INSERT INTO [asUsers] ([asName], [asPhone], [asRole], [asSex]) VALUES (@asName, @asPhone, @asRole, @asSex)"
        SelectCommand="SELECT [asId], [asName], [asPhone], [asRole], [asSex] FROM [asUsers]"
        UpdateCommand="UPDATE [asUsers] SET [asName] = @asName, [asPhone] = @asPhone, [asRole] = @asRole, [asSex] = @asSex WHERE [asId] = @asId"
         DeleteCommand="DELETE FROM [asUsers] WHERE [asId] = @asId" 
        
        >
        <DeleteParameters>
            <asp:Parameter Name="asId" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="asName" Type="String" />
            <asp:Parameter Name="asPhone" Type="Int32" />
            <asp:Parameter Name="asRole" Type="Int32" />
            <asp:Parameter Name="asSex" Type="Int32" />
            <asp:Parameter Name="asId" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="asName" Type="String" />
            <asp:Parameter Name="asPhone" Type="Int32" />
            <asp:Parameter Name="asRole" Type="Int32" />
            <asp:Parameter Name="asSex" Type="Int32" />
        </InsertParameters>
    </asp:SqlDataSource>cs代码中GridView的更新代码如下 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
        SqlCommand cmd = new SqlCommand();        string namestr = "", role = "", asid = "", phone = "", Sex = "";
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        TextBox Name = (TextBox)row.FindControl("TextBox10");
        DropDownList rol = (DropDownList)row.FindControl("DropDownList1");
        TextBox pho = (TextBox)row.FindControl("TextBox2");
        Label sex = (Label)row.FindControl("Label5");
        asid = GridView1.DataKeys[e.RowIndex].Value.ToString();
        namestr = Name.Text.Trim();
        role = rol.SelectedValue.ToString();
        phone = pho.Text.Trim();
        Sex = sex.Text.Trim();        string str = "update asUsers set asName='"+namestr+"',asSex='"+Sex+"',asRole='"+role+"',asPhone='"+phone+"'where asId='"+asid+"'";
        cmd.CommandText = str;
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            if (cmd.ExecuteNonQuery() > 0)
            {
                System.Windows.Forms.MessageBox.Show("更新成功!");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("更新失败!");
            }
        }
        catch (Exception ee)
        {
            System.Windows.Forms.MessageBox.Show(ee.ToString());
        }
        finally
        {
            con.Close();
        }
        GridView1.EditIndex = -1;为什么每次这个更新方法刚刚结束,就报错:asRole不能为空,但是数据库中的数据又是更新老的

解决方案 »

  1.   

    System.Windows.Forms.MessageBox.Show(ee.ToString());=======太强老, 又见 Web 中使用 WinForm
      

  2.   

    整个过程 很杂乱, 既然用到了 Sqldatasource  也用到了自带 各种 更新 添加 删除 命令  为何有 在代码中 从写一个 更新方法呢? 并且一个很简单的 事 你把它弄的 过与复杂. 要不用sqldatasource 里的 更新方法, 要不自己编写 代码.
    下面写一个 简单的例子:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    sqlcon=new SqlConnection(strcon);//前面省略
    string sql="update table set name='"+((TextBox)(GridView1.Rows[e.RowIndex].Controls[0])).Text.ToString().Trim()+"',sex='"+"',Where id='"+GridView1.DataKeys[e.RowIndex].Value.Tostring()+"'";
    SqlCommand com=new SqlCommand(sql,sqlcon);
    sqlcon.open();
    com.ExecuteNonQuery();
    sqlcon.close();
    GridView1.Databind();
    }要不直接用UpdateCommand 命令:UpdateCommand="Update table set name=@name,sex=@sex where id=@id"
    其实 这样写 就够啦,这样就可以直接 在GridView 上面 更新如果想 用自己指定的 控件里的 值更新 GridView
    再添加代码
    <UpdataParamter>
    <asp:ControlParamter ControlId="Name1" name="name" Property="Text"/>
    .......
    ......
    <UpdataParamter>当然还有好多种 方法,还是自己慢慢 学习吧,上面只是大概写写,希望能对你 有点帮助.
      

  3.   

    既然已经用了 SqlDataSource的UpdateCommand,就不用自己手写代码更新了,只要让那些参数和你的控件关联即可