using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using sp.aspx.cs;//导于自定义数据库连接的类
namespace sp.aspx
{
/// <summary>
/// manage 的摘要说明。
/// </summary>
public class manage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected conn str_conn=new conn();//声明连接类的对象,用构造函数付初值
private void Page_Load(object sender, System.EventArgs e)
{

SqlConnection objConnection=new SqlConnection(str_conn.StrConnection);
string strSQL="select * from ca_sq";
SqlDataAdapter objDataAdapter=new SqlDataAdapter(strSQL,objConnection);
DataSet objDateSet=new DataSet();
objDataAdapter.Fill(objDateSet,"Employees");
DataGrid1.DataSource=objDateSet;
DataGrid1.DataBind();
objConnection.Close();
objDateSet.Clear();

    
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
} private void Button1_Click(object sender, System.EventArgs e)
{

} private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   if (e.CommandName == "sqtg")
   {  
     
   if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked==true)
   {
   SqlConnection objConnection1=new SqlConnection(str_conn.StrConnection);
   objConnection1.Open();
   string strSQL1="update ca_sq set sq_sh='2'";
                   SqlCommand myCommand=new SqlCommand(strSQL1,objConnection1);
   myCommand.ExecuteNonQuery();
   objConnection1.Close();
    }
            }
        } }出现错误:
一运行,数据不能进入数据库,数据没有反应
}

解决方案 »

  1.   

    你的page_load()中的 objConnection1.Open();呢?
      

  2.   

    你能不能把问题提得细一些,比如把sp.aspx.cs命名空间中的你自己定义的数据库连接类conn 让我们看一看,很可能问题就出在这个类中
      

  3.   

    objDateSet.Clear();
    你这句语句要干什么DataGrid1.DataSource=objDateSet.Tables(0);
    DataGrid1.DataBind();
      

  4.   

    using System;namespace sp.aspx.cs
    {
    /// <summary>
    /// conn 的摘要说明。
    /// </summary>
    public class conn
    {
    private string strconnection;
    public conn()
    {
    strconnection="server=对外服务器;database=ca;uid=sa;password=*******;";
    }
    public string StrConnection
    {
    get
    {
    return strconnection;
    }
    set
    {
    strconnection=value;
    }
    }
    }
    }
      

  5.   

    private void Page_Load(object sender, System.EventArgs e)
    {

    SqlConnection objConnection=new SqlConnection(str_conn.StrConnection);
    string strSQL="select * from ca_sq";
    SqlDataAdapter objDataAdapter=new SqlDataAdapter(strSQL,objConnection);
    objConnection.Open();
    DataSet objDateSet=new DataSet();
    objDataAdapter.Fill(objDateSet,"Employees");
    objConnection.Open();
    DataGrid1.DataSource=objDateSet;
    DataGrid1.DataBind();

        
    }
      

  6.   

    第二个 objConnection.Open(); 是 objConnection.Close();
      

  7.   

    <%@ Page language="c#" Codebehind="manage.aspx.cs" AutoEventWireup="false" Inherits="sp.aspx.manage" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>manage</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
    <form id="manage" method="post" runat="server">
    <FONT face="宋体">
    <P align="center"><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" ShowFooter="True">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="选择">
    <ItemTemplate>
    <asp:CheckBox id="Checkbox2" runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="sq_rid" HeaderText=" 用户ID"></asp:BoundColumn>
    <asp:BoundColumn DataField="sq_ren" HeaderText="姓名"></asp:BoundColumn>
    <asp:BoundColumn DataField="sq_mc" HeaderText="标题"></asp:BoundColumn>
    <asp:BoundColumn DataField="sq_nr" HeaderText="内容"></asp:BoundColumn>
    <asp:HyperLinkColumn Text="查看" Target="_blank" DataNavigateUrlField="id" DataNavigateUrlFormatString="View.aspx?RequestID={0}" HeaderText="查看"></asp:HyperLinkColumn>
    <asp:TemplateColumn HeaderText="是否通过申请">
    <ItemTemplate>
    <asp:Label id="Label3" runat="server"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button id="Button1" runat="server" Text="申请" CommandName="sqtg"></asp:Button>
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="是否通过审批">
    <ItemTemplate>
    <asp:Label id="Label2" runat="server">Label</asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    </asp:datagrid></P>
    <P align="center">&nbsp;</P>
    </FONT>
    </form>
    </body>
    </HTML>
    这是前台的代码
      

  8.   

    就是数据库写数据进去不了
    还有就是我如果
    private void Page_Load(object sender, System.EventArgs e)
    {  if(!IsPostBack)
                         {     

    SqlConnection objConnection=new SqlConnection(str_conn.StrConnection);

    string strSQL="select * from ca_sq";
    SqlDataAdapter objDataAdapter=new SqlDataAdapter(strSQL,objConnection);
    objConnection.Open();
        DataSet objDateSet=new DataSet();
    objDataAdapter.Fill(objDateSet,"Employees");
    objConnection.Close();
        DataGrid1.DataSource=objDateSet;
    DataGrid1.DataBind();
    }


        
    }
    加上if语句,把if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked==true)去世掉,就能把表中的字段全部写进去
      

  9.   

    错误是在if (e.CommandName == "sqtg")这个地方,CommandName为"sqtg"的是DataGrid中的一个模板列中的一个Button的,而不是DataGrid的,所以你在DataGrid的ItemCommand中加入if (e.CommandName == "sqtg")是为false的,所以也就更新不了了。
      

  10.   

    刚才看错了,错误不在那里,而是在你的Page_Load中,你把DataGrid的绑定放到if(!IsPostBack)之中,如下:
    private void Page_Load(object sender, System.EventArgs e)
    {
       if(!IsPostBack)
      {
         SqlConnection objConnection=new SqlConnection(str_conn.StrConnection);
    string strSQL="select * from ca_sq";
    SqlDataAdapter objDataAdapter=new SqlDataAdapter(strSQL,objConnection);
    DataSet objDateSet=new DataSet();
    objDataAdapter.Fill(objDateSet,"Employees");
    DataGrid1.DataSource=objDateSet;
    DataGrid1.DataBind();
    objConnection.Close();
    objDateSet.Clear();
      }
      

  11.   

    老师,请问世为什么要加个IF语句子啊,如果加了这个语句子,如果不删if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked==true)这语句子,训会出现如下错误:
    “/sp”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 78:     {  
    行 79:       
    行 80:     if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked==true)
    行 81:     {
    行 82:     SqlConnection objConnection1=new SqlConnection(str_conn.StrConnection);
     源文件: d:\长安铃木模块\sp\aspx\manage.aspx.cs    行: 80 
      

  12.   

    你注意检查一下你的aspx文件中放的checkbox的id是"checkbox2",而在cs文件中你却是if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked.....,它找不到CheckBox1,所以就出错啊,你只要改一下就行了
      

  13.   

    如果删了if(((CheckBox)e.Item.FindControl("CheckBox1")).Checked==true)checkbox.checked的判断就没用了,全部的那列的字段都会被修改
      

  14.   

    看到上面的解释了吧?如果不判断是否为PostBack的话,你每次点击按钮出发的网页回传会再一次绑定DataGrid,所以你选择的也就无效了,所以checkbox都不会选中,也就无法更新了
      

  15.   

    知道了,但我把ID改为checkbox2,还是出那种错
      

  16.   

    “/sp”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 78:     {  
    行 79:       
    行 80:     if(((CheckBox)e.Item.FindControl("checkaa")).Checked==true)
    行 81:     {
    行 82:     SqlConnection objConnection1=new SqlConnection(str_conn.StrConnection);
     源文件: d:\长安铃木模块\sp\aspx\manage.aspx.cs    行: 80 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       sp.aspx.manage.DataGrid1_ItemCommand(Object source, DataGridCommandEventArgs e) in d:\长安铃木模块\sp\aspx\manage.aspx.cs:80
       System.Web.UI.WebControls.DataGrid.OnItemCommand(DataGridCommandEventArgs e) +110
       System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +66
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
       System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
       System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +121
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1263 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET 框架版本:1.0.3705.0; ASP.NET 版本:1.0.3705.0
      

  17.   

    你那个Button是放在DataGrid中的Footer里的,所以你点击Button触发时间时,判断if (e.CommandName == "sqtg"),符合条件的只有处于Footer的Item,处于Footer的Item是没有CheckBox的,所以会出异常。
    解决方法:把Button放在<ItemTemplate></ItemTemplate>之中。当然,Page_Load中也别忘了加入if(!IsPostBack)