最近在做网页的时候发现个问题,整了好久都不能解决!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不能为空,但是数据库中的数据又是更新老的
如下<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不能为空,但是数据库中的数据又是更新老的
解决方案 »
- 真正的程序员是逼出来的,是折磨出来的
- 请问前辈 ASPX错误页无效 标题要长才引人注意!
- Repeater控件的用发
- 100万条数据更新的性能问题?
- 我用VS.NET2003建了一个工程,当DEBUG后,IE打开了一个BAIDU链接(里面的搜索内容是localhost),然后提示HTTP/1.1 500 Internal Server Error
- 小电影网站的问题。
- [讨论]我想知道招聘中的精通到底是什么程度!
- 选中Checkbox,弹出新窗口,并让隐藏的textbox显示出来,如何实现?
- FormsAuthentication.SignOut()
- 求《ASP.NET高级编程》下载地址,在线等待,下载结帐
- 路过的高手请帮个忙!!!!
- 问个TabContainer的问题
下面写一个 简单的例子:
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>当然还有好多种 方法,还是自己慢慢 学习吧,上面只是大概写写,希望能对你 有点帮助.