、、、、、、
objConnection.Open();
、、、、、、
objConnection.Close();
要这样打开关闭,是成对出现的

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3557/3557985.xml?temp=.5724298
      

  2.   

    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data" %>
    <html>
      <body>
        <form method=post runat="server">
          <asp:DataGrid runat="server" ID="Datagrid1"
            DataKeyField="EmployeeID"
            OnEditCommand="Datagrid1_Edit"
            OnUpdateCommand="Datagrid1_Update"
            OnCancelCommand="Datagrid1_Cancel"
            OnDeleteCommand="Datagrid1_Delete"
            AutoGenerateColumns="False"
            Width="100%"
            HeaderStyle-Font-Size="10"
            HeaderStyle-Font-Bold="true"
            HeaderStyle-ForeColor="Red"
            HeaderStyle-BackColor="Yellow"
            HeaderStyle-BorderColor="Red"
            HeaderStyle-BorderWidth="5"
            FooterStyle-BorderColor="Red"
            FooterStyle-BorderWidth="5"    
            ItemStyle-BackColor="LightCyan"
            ItemStyle-ForeColor="DarkBlue"        
            AlternatingItemStyle-BackColor="LightYellow"
            AlternatingItemStyle-ForeColor="Maroon">
            <Columns>
              <asp:EditCommandColumn ItemStyle-Width="25px"
                EditText="<img border=0 alt='Edit record' src=Edit.gif>"
                CancelText="<img border=0 alt='Cancel changes' src=Cancel.gif>"
                UpdateText="<img border=0 alt='Accept changes'  src=OK.gif>"
              />
    <asp:ButtonColumn ItemStyle-Width="25px" ButtonType="LinkButton"
      Text="<img border=0 src=Delete.gif>" CommandName="delete"
    />
              <asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID" ReadOnly="true" />
              <asp:TemplateColumn HeaderText="Title" ItemStyle-Width="50px">
                <ItemTemplate>
                  <%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") %>          
                </ItemTemplate>
                <EditItemTemplate>
                  <asp:DropDownList runat="server" ID="EditTitle" DataSource='<%# TitlesOfCourtesy %>'
                    SelectedIndex='<%# GetSelectedTitle(DataBinder.Eval(Container.DataItem, "TitleOfCourtesy")) %>' />
                </EditItemTemplate>
              </asp:TemplateColumn>
              <asp:TemplateColumn HeaderText="Name">
                <ItemTemplate>
                  <b><%# DataBinder.Eval(Container.DataItem, "LastName") %></b>,
                  <%# DataBinder.Eval(Container.DataItem, "FirstName") %>          
                </ItemTemplate>
                <EditItemTemplate>
                  <asp:TextBox runat="server" ID="EditLastName" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>' />
                  <asp:TextBox runat="server" ID="EditFirstName" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
                </EditItemTemplate>
              </asp:TemplateColumn>
              <asp:BoundColumn HeaderText="City" ItemStyle-Width="150px" DataField="City" />
              <asp:TemplateColumn HeaderText="USA?" ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                  <asp:CheckBox runat="server" Enabled="false"
                    Checked='<%# DataBinder.Eval(Container.DataItem, "Country").ToString() == "USA" %>' />
                </ItemTemplate>
              </asp:TemplateColumn>
            </Columns>
          </asp:DataGrid>
        </form>
      </body>
    </html><script runat="server" language="C#">
      public string[] TitlesOfCourtesy 
      {
        get {
          return new string[4]{"Mr.", "Dr.", "Ms.", "Mrs."};
        }
      }
      
      int GetSelectedTitle(object title)
      {
        return Array.IndexOf(TitlesOfCourtesy, title.ToString());
      }
      
      void Page_Load()
      {
        if (!Page.IsPostBack)
          BindGrid();
      }
      
      void BindGrid()
      {
        // create the command and the connection
        string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
        string sql = @"SELECT * FROM Employees";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(sql, conn);
          
        // open the connection and get the Reader
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
               
        // bind the reader to the DataList
        Datagrid1.DataSource = reader;
        Datagrid1.DataBind();
          
        // close the reader and the connection
        reader.Close();
        conn.Close();
      }
      
      void Datagrid1_Edit(Object sender, DataGridCommandEventArgs e)
      {
        Datagrid1.EditItemIndex = (int)e.Item.ItemIndex;
        BindGrid();
      }
      
      void Datagrid1_Cancel(Object sender, DataGridCommandEventArgs e)
      {
        Datagrid1.EditItemIndex = -1;
        BindGrid();
      }  void Datagrid1_Update(Object sender, DataGridCommandEventArgs e)
      {
        // get the ID of the record to update
        int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];
        
        // get the references to the edit controls
        DropDownList title = (DropDownList)e.Item.FindControl("EditTitle");
        TextBox lastName = (TextBox)e.Item.FindControl("EditLastName");
        TextBox firstName = (TextBox)e.Item.FindControl("EditFirstName");
        TextBox city = (TextBox)e.Item.Cells[5].Controls[0];
        
        // create the connection and the UPDATE command
        string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
        string sql = @"UPDATE Employees SET TitleOfCourtesy = @TitleOfCourtesy, 
          LastName = @LastName, FirstName = @FirstName, City = @City WHERE EmployeeID = @EmployeeID";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        
    // create the required parameters
        cmd.Parameters.Add(new SqlParameter("@TitleOfCourtesy", SqlDbType.NVarChar, 25));
    cmd.Parameters["@TitleOfCourtesy"].Value = title.SelectedItem.Text.Trim();
        cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 20));
    cmd.Parameters["@LastName"].Value = lastName.Text.Trim();
    cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 10));
    cmd.Parameters["@FirstName"].Value = firstName.Text.Trim();
    cmd.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15));
    cmd.Parameters["@City"].Value = city.Text.Trim();
    cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
    cmd.Parameters["@EmployeeID"].Value = empID;

        // execute the command
        cmd.Connection.Open();
        try {
          cmd.ExecuteNonQuery();
        }
        catch (SqlException) {
          // handle exception... 
        }
        finally { 
          cmd.Connection.Close();
        }
        
        // stop the editing and rebind the grid
        Datagrid1.EditItemIndex = -1;
        BindGrid();
      }
      
      void Datagrid1_Delete(Object sender, DataGridCommandEventArgs e)
      {
        // get the ID of the record to update
        int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];
           
        // create the connection and the DELETE command
        string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
        string sql = @"DELETE FROM Employees WHERE EmployeeID = " + empID.ToString();
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        
        // execute the command
        cmd.Connection.Open();
        try {
          cmd.ExecuteNonQuery();
        }
        catch (SqlException) {
          // handle exception... 
        }
        finally { 
          cmd.Connection.Close();
        }
        
        // rebind the grid
        Datagrid1.EditItemIndex = -1;
        BindGrid();
      }
    </script>