Jacman
Jacman

Reputation: 1582

How to retrieve asp:CheckBox value from SQL db?

I have a table of CheckBoxes that are inserted into a SQL db as 'True' and 'False'. However, I would like to retrieve those values again with a load event, but I'm not able to get them. This is my code:

protected void Page_Load(object sender, EventArgs e)
{
    if (auditChecklist != null)
    {
        //for loading audit checklist
        getAuditChecklist();
    }

}

I tried to retrieve them with the function below, which I thought it might do it:

    private void getAuditChecklist()
{

    //int i = 0;
    SqlCommand cmd = null;
    string conn = ConfigurationManager.ConnectionStrings["PSCV1ConnectionString"].ConnectionString;
    string queryString = @"SELECT * FROM AUDIT_CHECKLIST " +
        "WHERE SITE_ID = @SiteID";

    using (SqlConnection connection =
               new SqlConnection(conn))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();
        cmd = new SqlCommand(queryString);
        cmd.Connection = connection;            

        cmd.Parameters.Add(new SqlParameter("@SiteID", //the name of the parameter to map
              System.Data.SqlDbType.NVarChar, //SqlDbType value
              20, //The width of the parameter
              "Site_ID")); //The name of the column source
        //Fill the parameter with the value retrieved
        //from the text field
        cmd.Parameters["@SiteID"].Value = foo.Site_ID;

        SqlDataReader reader = cmd.ExecuteReader();

        if (CheckBox1.Checked == true)
        {                
            while (reader.Read())
            {
            cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
               System.Data.SqlDbType.NVarChar, //SqlDbType value
               20, //The width of the parameter
               "Mount")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@Mount"].Value = "True";
            }
        }
        else
        {
            cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
               System.Data.SqlDbType.NVarChar, //SqlDbType value
               20, //The width of the parameter
               "Mount")); //The name of the column source
            //Fill the parameter with the value retrieved
            //from the text field
            cmd.Parameters["@Mount"].Value = "False";

        }
        reader.Close();
    }
}

Thanks a lot for the help!

Upvotes: 0

Views: 6587

Answers (3)

Jacman
Jacman

Reputation: 1582

This how I did to read/get checkmark values from the database:

    private void getAuditChecklist()
{
SqlCommand cmd = null;
string conn =    ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string queryString = @"SELECT Mount, Braker, Access, Conn_Net, Log_Book, Pictures, Floor, Cb_Lenght, Channel FROM AUDITOR_CHECKLIST " +
    "WHERE SITE_ID = @SiteID";        

using (SqlConnection connection =
           new SqlConnection(conn))
{
    SqlCommand command =
        new SqlCommand(queryString, connection);
    connection.Open();
    cmd = new SqlCommand(queryString);
    cmd.Connection = connection;

    cmd.Parameters.Add(new SqlParameter("@SiteID", //the name of the parameter to map
          System.Data.SqlDbType.NVarChar, //SqlDbType value
          20, //The width of the parameter
          "SITE_ID")); //The name of the column source
    //Fill the parameter with the value retrieved
    //from the text field
    cmd.Parameters["@SiteID"].Value = foo.Site_ID;

    SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
 {                    
CheckBox1.Checked = (reader.GetBoolean(reader.GetOrdinal("Mount")));
CheckBox2.Checked = (reader.GetBoolean(reader.GetOrdinal("Braker")));
CheckBox3.Checked = (reader.GetBoolean(reader.GetOrdinal("Access")));
CheckBox4.Checked = (reader.GetBoolean(reader.GetOrdinal("Conn_Net")));
CheckBox5.Checked = (reader.GetBoolean(reader.GetOrdinal("Log_Book")));
CheckBox6.Checked = (reader.GetBoolean(reader.GetOrdinal("Pictures")));
CheckBox8.Checked = (reader.GetBoolean(reader.GetOrdinal("Floor")));
CheckBox9.Checked = (reader.GetBoolean(reader.GetOrdinal("Cb_lenght")));
CheckBox10.Checked = (reader.GetBoolean(reader.GetOrdinal("Channel")));
 } 
reader.Close();
 }        
}

Upvotes: 0

Adeel Shekhani
Adeel Shekhani

Reputation: 1105

If I got your Question properly...The problem is in your datatypes. I think its a bit field.

sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommmand.Parameters["@Mount"].Value = 1;

Upvotes: 1

mccee
mccee

Reputation: 1667

You mention that your values are inserted as 'True' and 'False', yet you are passing 1 and 0 into an nvarchar field. Is the database field a nvarchar or a bit?

If it's a bit field, change you parameter to a bit data type:

sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommand.Parameters["@Mount"].Value = 1;

If it's an nvarchar field, you should rethink your schema because you should really be using a bit data type. But, for the sake of the question, you could do that like so:

sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.NVarChar));
sqlCommand.Parameters["@Mount"].Value = "True";

However, you are adding the @Mount parameter, but your queryString makes no reference to a @Mount parameter. I'm not sure what your intention is with that, but I imagine that is where your issue lies.

I would recommend you start HERE to begin reading data from the database.

Upvotes: 0

Related Questions