CheckBox不发送检查值以访问数据库C#Asp

时间:2016-04-27 00:36:44

标签: c# ms-access

这是我的C#代码,我的问题是标题是我的复选框值没有进入我的访问数据库,或者至少没有更改它们。

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

2 个答案:

答案 0 :(得分:0)

如果您要附加Access Yes / No字段,那么我会尝试从第二个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() + ")";

答案 1 :(得分: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() + "')";