Share via


getting values from checkboxes and saving them to datadase in c#

Question

Friday, December 24, 2010 4:57 AM

Hai to all

Can any one plz tell me how to save data from checkedboxes to database

i am doing for windows application

my database providers r Microsoft.Jet.OLEDB.4.0

plz help me

All replies (6)

Friday, December 24, 2010 6:11 AM ✅Answered

hi,

jst an example....

Create a Table :

Sports:

SportsID      int
SportsName   nvarchar(50)
IntrestToPlay   bit


C#

  bool cricket = false;
  if(chkCricket.IsChecked.Value)
  {
   cricket = true;
  }
  InsertSportsDetails("Cricket", cricket);

  bool football = false;
  if(chkFootball.IsChecked.Value)
  {
   football = true;
  }
  InsertSportsDetails("Football", football);


 public int InsertSportsDetails(string sportsName, bool intrestToPlay)
 { 

   OleDbConnection cnConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";User Id=" + sUserName + ";Password=" + sPassword + ";");
   cnConnection.Open();

   OleDbCommand cmd = new OleDBCommand();
   cmd.Connection = cnConnection ;
   cmd.CommandText = "INSERT INTO SPORTS (SportsSName,IntrestToPlay) VALUES ('"+sportsName+"', "+intrestToPlay+")";
 
   int res = cmd.ExecuteNonQuery(); 

   cnConnection.Close();
   cnConnection.Dispose();

   return res;
 }

Nagarjuna Dilip


Friday, December 24, 2010 6:19 AM ✅Answered

Hai darnold

Can u explain it clearly

i am fresh to this c# and this project also 

plz tell me wher should i write this Condition

 

Sorry by mistake i clicked on proposed as answer link . i was willing to click over quote. nyway, Here's solution

void getvalues(ListM<CheckBox> chk)

{

    for(int i=0; i<chk.Length;i++)
{
if(chk[i].Checked)
{
string values=chk[i].property;// Property is the property of checkbox in which you have saved your data.

}
}

}

The function takes list of checkboxes which contain all checkboxes under consideration.

or if you want to do it on  time of  checking Checkbox then

void Check_Cheked(object sender,EventArgs e)
{
CheckBox chk= (CheckBox) sender;
if(chk.Checked)
{
string value=chk.property;
}
}

Now you have got the string and can save that in database using insert or update query.

Hope it'll help

Regards Kumar Gaurav.


Friday, December 24, 2010 5:08 AM

On 12/23/2010 11:57 PM, jayachand wrote:

> Hai to all

>

> Can any one plz tell me how to save data from checkedboxes to database

>

> i am doing for windows application

>

> my database providers r Microsoft.Jet.OLEDB.4.0

>

> plz help me

>

 

The type definition is a smallint of the table definition. You set it

to 1 to equal true, and you set it to 0 equal false. That's how you

would look at the data coming from the data too. If the smallint is = 1,

then is true and checkbox to checked. If the smallint is set to 0, then

it's false and checkbox to unchecked.

 

Smallint in code is int16.

 


Friday, December 24, 2010 5:48 AM

Hai darnold

Can u explain it clearly

i am fresh to this c# and this project also 

plz tell me wher should i write this Condition

 


Friday, December 24, 2010 10:26 AM

On 12/24/2010 12:48 AM, jayachand wrote:

> Hai darnold

>

> Can u explain it clearly

>

> i am fresh to this c# and this project also

>

> plz tell me wher should i write this Condition

>

 

 

int16 checkedValue = 0

 

if (checkbox.value == 1)

{

 

checkedvalue = 1

 

}

 

 

or

 

checkedValue = checkbox.value;

 

 

You should write checkedvalue to the database table when you write the

record in a routine that writes the record.

 


Wednesday, December 29, 2010 10:33 AM

Hi jayachand,

Welcome to the MSDN Forum.

I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
 
If you have got some good solutions to this problem? please mark the useful reply as answer. If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

Thank you for your understanding and support.

I look forward to hearing from you.

 

Best Regards,

Mio

Mio Miao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.