Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
i have to insert value in the SQL server 2008 database using checkbox.
so please help me for this.
Posted
Comments
Aditya Mangipudi 14-Aug-12 14:38pm    
Please show what you have done so far.
Kenneth Haugland 14-Aug-12 14:39pm    
And normally you send things to the SQL server with witch code? Can we see you rattemts. Sending Boolean values is not different to sending strings numbers etc..
StianSandberg 14-Aug-12 16:49pm    
Is it?
Kenneth Haugland 14-Aug-12 16:55pm    
Ehh... I meant not.... Its missing... doh
StianSandberg 14-Aug-12 17:02pm    
:)

The Answer:

XML
<form id="form1" runat="server">
     <asp:checkbox id="chkBox" runat="server" text="Agreed" xmlns:asp="#unknown">
                              oncheckedchanged="chkBox_CheckedChanged" />
</form>
</asp:checkbox>


C#
protected void chkBox_CheckedChanged(object sender, EventArgs e)
    {
        SqlConnection conn = null;

        try
        {
            using (conn=new SqlConnection("YourConnectionString"))
            {
                String strSQL=String.Empty;
                strSQL=String.Format("INSERT Test(Agrement) VALUES({0})",chkBox.Checked);

                SqlCommand cmd=new SqlCommand();
                cmd.Connection=conn;
                cmd.CommandText=strSQL;
                cmd.CommandType=System.Data.CommandType.Text;

                int intResult=cmd.ExecuteNonQuery();
            }

        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
            conn=null;
        }
    }


Thanks,

Please read my newly posted article and Plz comments or vote.
A Framework for Software Application[^]
 
Share this answer
 
Comments
ridoy 14-Aug-12 15:13pm    
right one.
Abdul Quader Mamun 14-Aug-12 15:28pm    
ok accept answer and vote!
Mohamed Mitwalli 15-Aug-12 2:37am    
I vote 3 , SQL injection you have to use parameters, there is no need to write cmd.CommandType =System.Data.CommandType.Text; it's already the default , you didn't dispose your command Hope it help .
Abdul Quader Mamun 15-Aug-12 5:05am    
Please what was the question? If you afraid SQL inject then you should write Store procedure. here is only solution for according to he question.
Mohamed Mitwalli 15-Aug-12 6:37am    
Hi abdul Quader
Well i think that way will be much better
using (conn = new SqlConnection("YourConnectionString"))
{
conn.open();
using (SqlCommand cmd = new SqlCommand("INSERT Test(Agrement) VALUES(@Agrement)", conn))
{
cmd.Parameters.AddWithValue("@Agrement", chkBox.Checked);
int intResult = cmd.ExecuteNonQuery();
}
}
Ramdan kreem :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900