2011-07-11 104 views
0

我試圖寫入數據庫,每當一個複選框被選中或取消選中。如果複選框被選中,我們希望能夠存儲1(比特值),如果不是,我們可以存儲0。這樣,如果它已被選中,當表格加載時,我們將在複選框中看到一個檢查。否則,不會有一個。以下是我迄今爲止:更新數據庫位值

C# 
    public partial class vieworders : System.Web.UI.Page 
{ 
    private string orderByString; 
    private string fieldString; 
    private string address; 
    private DataGrid dataGrid = new DataGrid(); 
    SqlDataAdapter dataAdapter; 
    DataSet dataSet; 


    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      orderByString = orderByList.SelectedItem.Value; 
      fieldString = searchTextBox.Text; 
      string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString; 
      //////////////////////////// 




      connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

      //TEST 
      for (int i = 0; i < DefaultGrid.Rows.Count; i++) 
      { 
       CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess"); 
       if (chkUpdate != null) 
       { 
        OrderBrowser.Text += "Test"; 
       } 
      } 



      // Create an SqlConnection to the database. 
      // Create an SqlConnection to the database. 
      using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
      { 
       connection.Open(); 

       dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection); 

       // create the DataSet 
       dataSet = new DataSet(); 
       // fill the DataSet using our DataAdapter    
       dataAdapter.Fill(dataSet, "SecureOrders"); 

       DataView source = new DataView(dataSet.Tables[0]); 
       DefaultGrid.DataSource = source; 
       DefaultGrid.DataBind(); 
       connection.Close(); 
      } 
     } 


     } 




    protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e) 
    { 
     GridViewRow row = DefaultGrid.SelectedRow; 
     string name = "Name: " + row.Cells[2].Text + " " + row.Cells[3].Text + "\r\n"; 
     // if (row.Cells[4].Text == "&nbsp;") 
     //{ 
      //address = "Address: " + row.Cells[3].Text + "\r\n   " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n"; 

     // } 
     //else 
     // { 
      // address = "Address: " + row.Cells[3].Text + "\r\n   " + row.Cells[4].Text + "\r\n   " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n"; 
     //} 

     string zip = "Zip: " + row.Cells[4].Text + "\r\n"; 
     string email = "Email: " + row.Cells[5].Text + "\r\n"; 
     //string phone = "Phone: " + row.Cells[10].Text + "\r\n"; 
     //string cctype = "Credit Card Type: " + row.Cells[11].Text + "\r\n"; 
     //string ccnum = "Credit Card Number: " + row.Cells[12].Text + "\r\n"; 
     //string ccexp = "Credit Card Expiration: " + row.Cells[13].Text + "\r\n"; 
     string length = "Length: " + row.Cells[8].Text + "\r\n"; 
     //string delivery = "Delivery: " + row.Cells[15].Text + "\r\n"; 
     string price = "Price: " + row.Cells[7].Text + "\r\n"; 
     string source = "Source: " + row.Cells[6].Text + "\r\n"; 
     //string joined = "Joined: " + row.Cells[18].Text + "\r\n"; 
     //string url = "URL: " + row.Cells[19].Text + "\r\n"; 

     OrderBrowser.Text = name + email + length + price + source; 
    } 

    protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox cb = (CheckBox)sender; 
     GridViewRow gr = (GridViewRow)cb.NamingContainer; 




     connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

     using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
     { 
      connection.Open(); 


      dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection); 

      // create the DataSet 
      dataSet = new DataSet(); 
      // fill the DataSet using our DataAdapter    
      dataAdapter.Fill(dataSet, "SecureOrders"); 
      DataSet myDSchanged = dataSet.GetChanges(DataRowState.Modified); 
      SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter); 

      dataAdapter.Update(myDSchanged, "SecureOrders"); 
      dataSet.AcceptChanges(); 
      connection.Close(); 
     } 




    } 

    } 

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="vieworders.aspx.cs" Inherits="Cabot3.custserv.vieworders" %> 

 <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true"> 
      <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem> 
      <asp:ListItem Value="lName">Last Name</asp:ListItem> 
      <asp:ListItem Value="state">State</asp:ListItem> 
      <asp:ListItem Value="zip">Zip Code</asp:ListItem> 
      <asp:ListItem Value="cwaSource">Source</asp:ListItem> 
      <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem> 
     </asp:DropDownList> 
    </div> 
    <div> 
     <asp:Label runat="server" ID="searchLabel" Text="Search For: " /> 
     <asp:TextBox ID="searchTextBox" runat="server" Columns="30" /> 
     <asp:Button ID="searchButton" runat="server" Text="Search" /> 
    </div> 
<div> 
<asp:UpdatePanel ID = "up" runat="server"> 



    <ContentTemplate> 
    <div style= "overflow:auto; height:150px; width:700px"> 
    <asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName, lName, zip" 
    onselectedindexchanged = "DefaultGrid_SelectedIndexChanged" 
    autogenerateselectbutton = "true" 
    selectedindex="0"> 
    <SelectedRowStyle BackColor="Azure" 
    forecolor="Black" 
    font-bold="true" /> 
    <Columns> 
    <asp:TemplateField HeaderText="Processed"> 
       <ItemTemplate> 
        <asp:CheckBox ID="CheckBoxProcess" AutoPostBack = "true" Checked ='<%#Eval("processed") %>' OnCheckedChanged="CheckBoxProcess_CheckedChanged" runat="server" Enabled="true" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
    </Columns> 
    </asp:GridView> 
    </div> 
    </div> 
    <asp:TextBox ID="OrderBrowser" columns="70" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true"/> 
    </ContentTemplate> 
    </asp:UpdatePanel> 



</div> 
</form> 

任何人都可以引導我走向正確的方向嗎?

+0

這個家庭作業? –

+0

不,它不是!爲什麼,我做的事情完全錯誤? –

+0

當我說「我們」時,我的意思是我的公司。 –

回答

1

這是我爲你寫的一個輔助方法的靜態方法。它採用布爾值,並在列中設置0或1位值。你當然需要用你的表名,列名等來修改它。我只是創建了一個簡單的測試數據庫來向你展示如何做到這一點。

現在,假定該行已經存在於數據庫中。在我的測試數據庫我的表被命名爲 '設置',並有三列:

  • ID(INT)
  • 名稱(爲nvarchar(50))
  • 值(爲nvarchar(50))

行已經存在,ID爲0,名稱爲'CheckboxState',值爲0.因此,最初應該有某種機制將行放在數據庫中。

但是,基本上,你可以在代碼隱藏中調用它,並將CheckBox.Checked屬性傳遞給它。

static void UpdateCheckedState(bool state) { 
    string connectionstring = "<yourconnectionstring>"; 
    using (SqlConnection connection = new SqlConnection(connectionstring)) { 
     try { 
      connection.Open(); 
     } 
     catch (System.Data.SqlClient.SqlException ex) { 
      // Handle exception 
     } 
     string updateCommandText = "UPDATE Settings SET Value = @state WHERE Name = 'CheckboxState'"; 
     using (SqlCommand updateCommand = new SqlCommand(updateCommandText, connection)) { 
      SqlParameter stateParameter = new SqlParameter("state", state); 
      updateCommand.Parameters.Add(stateParameter); 
      try { 
       updateCommand.ExecuteNonQuery(); 
      } 
      catch (System.Data.SqlClient.SqlException ex) { 
       // Handle exception 
      } 
     } 
    } 
} 
+0

謝謝 - 我要試一試。數據庫中的字段是「已處理」的,列ID是「CheckBoxProcess」,並且它們全部被初始檢查,所以值爲1! –