简体   繁体   中英

radiobutton: how do i retrieve data from db

Access 2003 db

Field Name          Data Type  
CriminalOffence     Text

VS 2010 C#

Using a groupbox, I can store data from radiobutton into my db. I have databinded both groupbox and radiobuttons to my db. When I use my nativgation buttons I see the data that has been stored where the groupbox text is. So the problem is I don't know how to properly retrieve data for the radiobutton. Retrieving data from textbox and combox is showing as it should be. Also when I want to insert new data the radiobutton does not get cleared. So...

I have two radiobuttons named, 1) rBYes 2) rBNo

I have the following method for inserting record...

private void btnInsert_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1
                                    (ID, AgeGroup, Gender, CriminalOffence)   
                              VALUES(@txtID, @AcBAG, @cBGender, @CriminalOffence)", myCon);
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@ID", txtID.Text);
        cmd.Parameters.AddWithValue("@AgeGroup", cBAG.Text);
        cmd.Parameters.AddWithValue("@Gender", cBGender.Text);

        string str = "";
        if (rBYes.Checked)
        {
            str = "Yes";
        }
        if (rBNo.Checked)
        {
            str = "No";
        }
        cmd.Parameters.AddWithValue("@CriminalOffence", SqlDbType.NVarChar).Value =   
        str;
    }

And a method for creating a new record

 private void btnNew_Click(object sender, EventArgs e)
    {
        txtID.Text = "";
        cBAG.Text = "";
        cBGender.Text = "";
        rBYes.Text = "";
        rBNo.Text = "";
    }

An example of navigation button..

    private void btnNextRec_Click(object sender, EventArgs e)
    {
        this.table1BindingSource.MoveNext();
    }

ConnectionString...

    myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\..
    \Database1.mdb");

Please can someone help me here, thanks in advance

Parameter names must match the parameters in the SQL insert statement.

private void btnInsert_Click(object sender, EventArgs e)
{
  OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1
                                        (ID, AgeGroup, Gender, CriminalOffence)   
                                  VALUES(@ID, @AgeGroup, @Gender, @CriminalOffence)", myCon);
  cmd.CommandType = CommandType.Text;

  cmd.Parameters.AddWithValue("@ID", txtID.Text);
  cmd.Parameters.AddWithValue("@AgeGroup", cbAG.Text);
  cmd.Parameters.AddWithValue("@Gender", cbGender.Text);
  cmd.Parameters.AddWithValue("@CriminalOffence", ((rBYes.Checked)? "Yes":"No"));

  myCon.Open();
  cmd.ExecuteNonQuery();
  myCon.Close(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM