2016-04-27 77 views
1

這是我的C#代碼和我的問題作爲標題說是我的複選框值不會進入我的訪問數據庫,或至少不會改變它們。複選框不發送檢查值訪問數據庫C#Asp

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using System.Data; 
using System.Data.OleDb; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
     } 
     Label1.Text = (string)Session["sesionicontrol"]; 
    } 

    protected void txtPass_TextChanged(object sender, EventArgs e) 
    { 
    } 

    protected void check1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 

    protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     //Declare Variables 
     string username = txtEmailLogin.Text; 
     string password = txtPasswordLogin.Text; 
     username = username.Trim().ToLower(); 
     password = password.Trim().ToLower(); 

     //Handle null or empty fields 
     if ((string.IsNullOrEmpty(username)) || (string.IsNullOrEmpty(password))) 
     { 
      lblError.Text = "Please Enter a vaild Username or Password"; 
     } 
     else if (((username.Contains("@mu.edu") || (username.Contains("@marquette.edu"))))) 
     { 
      //Run select query and populate a table, then check to see if the user and pass are in that table 
      OleDbConnection conn = null; 

      DataTable dt = new DataTable(); 

      try 
      { 
       string connString = 
        ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
       conn = new OleDbConnection(connString); 
       string query = "Select Count(*) From Team Member Where Email = ? AND Pass = ?"; 
       OleDbCommand cmd = new OleDbCommand(query, conn); 
       conn.Open(); 
       cmd.CommandType = CommandType.Text; 
       OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
       da.Fill(dt); 
      } 
      catch (Exception ex) 
      { 
       // handle error here 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      //checking if there is a result in the virtual table, if there is they successfully logged in 
      if (dt.Rows.Count >= 0) 
      { 
       lblError.Text = "Welcome!"; 
       /// Take to Homepage 
       CommonClass.txtEmail = txtEmailLogin.Text; 
       Server.Transfer("HomePage.aspx", true); 
      } 
      else 
      { 
       lblError.Text = "Incorrect Username or Password"; 
      } 
     } 
    } 

    protected void btnRegister_Click(object sender, EventArgs e) 
    { 
     OleDbConnection conn = null; 
     DataTable gridTable = new DataTable(); 
     try 
     { 
      string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
      conn = new OleDbConnection(connString); 

      string query = "INSERT INTO [Team Member] (FirstName, LastName, Email, Pass, Age, Hobbies, FavoriteColor, Major) VALUES('" + txtFirst.Text + "','" + txtLast.Text + "', '" + txtEmail.Text + "','" + txtPass.Text + "','" + txtAge.Text + "','" + txtHobbies.Text + "', '" + txtFavorite.Text + "','" + txtMajor.Text + "')"; 
      string query1 = "INSERT INTO [Team Member] (Soccer, Basketball, Football, Softball) VALUES('" + c1.Checked.ToString() + "', '" + c2.Checked.ToString() + "', '" + c3.Checked.ToString() + "', '" + c4.Checked.ToString() + "')"; 

      OleDbCommand cmd = new OleDbCommand(query, conn); 
      conn.Open(); 

      cmd.CommandType = CommandType.Text; 
      cmd.ExecuteNonQuery(); 
      cmd.Dispose(); 
      lblError1.Text = ("Registered Successfully"); 
     } 
     catch (Exception ex) 
     { 
      lblError1.Text = ("Error occurred: " + ex.Message); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

    protected void btnReg_Click(object sender, EventArgs e) 
    { 
     txtFirst.Visible = !txtFirst.Visible; 
     txtLast.Visible = !txtLast.Visible; 
     txtEmail.Visible = !txtEmail.Visible; 
     txtPass.Visible = !txtPass.Visible; 
     txtPassConfirm.Visible = !txtPassConfirm.Visible; 
     btnRegister.Visible = !btnRegister.Visible; 
     btnReg.Visible = !btnReg.Visible; 

     c1.Visible = !c1.Visible; 
     c2.Visible = !c2.Visible; 
     c3.Visible = !c3.Visible; 
     c4.Visible = !c4.Visible; 

     txtAge.Visible = !txtAge.Visible; 
     txtHobbies.Visible = !txtHobbies.Visible; 
     txtFavorite.Visible = !txtFavorite.Visible; 
     txtMajor.Visible = !txtMajor.Visible; 

     lbl1.Text = "Sports you want to play"; 
     lbl2.Text = "Age"; 
     lbl3.Text = "Hobbies"; 
     lbl4.Text = "Favorite Color"; 
     lbl5.Text = "Major"; 
    } 

    protected void c2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 

    protected void c1_CheckedChanged(object sender, EventArgs e) 
    { 
    } 
} 

我的數據庫看起來像這樣 enter image description here

回答

0

如果要追加到訪問是/否領域的話,我會嘗試刪除單引號(')從第二INSERT INTO行:

string query1 = "INSERT INTO [Team Member] 
(Soccer, Basketball, Football,  Softball) 
VALUES(" + c1.Checked.ToString() + ", " 
+ c2.Checked.ToString() + ", " 
+ c3.Checked.ToString() + ", " 
+ c4.Checked.ToString() + ")"; 
+0

我會給這個去一個回報謝謝。 –

+0

這是答案,謝謝你。我想知道你是否可以回答更多的問題。我想運行一系列if語句來檢查複選框是否被選中,以及是否將用戶放入相應的數據庫(足球,籃球,足球,壘球)。這將在單擊註冊按鈕時完成。它必須支持一次添加到多個數據庫。我會爲每個做多個查詢嗎? –

+0

不知道如何工作,考慮到query1從未執行。更不用說2插入語句應該導致2行,這不是你想要的,我會認爲 – JSON

0

首先,您的複選框值永遠不會被插入的原因是因爲您的OleDbCommand是這樣定義的:

OleDbCommand cmd = new OleDbCommand(query, conn); 

使用query作爲command.text。 query1永遠不會被引用,因此永遠不會執行。

其次,更重要的是,您需要將insert語句作爲一個語句,而不是2.調用2個Insert語句會導致將2個行添加到表中。一個包含來自查詢的值,另一個包含來自query1的複選框值。你應該在這樣一個字符串中定義你的查詢

string query = "INSERT INTO [Team Member] " + 
     "(FirstName, LastName, Email, Pass, Age, Hobbies, FavoriteColor, Major, Soccer, Basketball, Football, Softball) " + 
     "VALUES('" + txtFirst.Text + "','" + txtLast.Text + "', '" + txtEmail.Text + "','" + txtPass.Text + "','" + 
     txtAge.Text + "','" + txtHobbies.Text + "', '" + txtFavorite.Text + "','" + txtMajor.Text + "','" + 
     c1.Checked.ToString() + "', '" + c2.Checked.ToString() + "', '" + c3.Checked.ToString() + "', '" + c4.Checked.ToString() + "')"; 
+0

我會讓更改並回報。謝謝 –