开发者

How to update Access tables using RadioButtons from a Windows Forms application in .NET?

I want to insert / update rows from an Access database, using RadioButton controls.

For eg.: Sex of worker (male / female)

I'm using a WinForms application written in C#.

My actual code is the following:

private OleDbConnection con;
private DataSet ds1;
private OleDbDataAdapter da;

int MaxRows = 0;
int inc = 0;

private void Form1_Load(object sender, EventArgs e)
{
    con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb");
    ds1 = new DataSet();

    //con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb";

    string sql = "SELECT * from tblWorkers";
    da = new OleDbDataAdapter(sql, con);

    con.Open();
    da.Fill(ds1, "MyWorkers1");
    NavigateRecords();
    MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
    //MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
    //MessageBox.Show("Database open");

    con.Close();
    //MessageBox.Show("Database close");

    //con.Dispose();
}

private void NavigateRecords()
{
    DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
    textBox1.Text = drow.ItemArray.GetValue(0).ToString();
    textBox2.Text = drow.ItemArray.GetValue(1).ToString();
    textBox3.Text = drow.ItemArray.GetValue(2).ToString();
}

private void btnNext_Click(object sender, EventArgs e)
{
    if (inc != MaxRows - 1)
    {
        inc++;
        NavigateRecords();
    }
    else
    {
        MessageBox.Show("No More Records");
    }
}

private void btnPrevious_Click(object sender, EventArgs e)
{
    if (inc > 0)
    {
        inc--;
        NavigateRecords();
    }
    else
    {
    开发者_如何学Go    MessageBox.Show("First Record");
    }
}

private void btnFirst_Click(object sender, EventArgs e)
{
    if (inc != 0)
    {
        inc = 0;
        NavigateRecords();
    }
}

private void btnLast_Click(object sender, EventArgs e)
{
    if (inc != MaxRows - 1)
    {
        inc = MaxRows - 1;
        NavigateRecords();
    }
}

private void btnAddNew_Click(object sender, EventArgs e)
{
    textBox1.Clear();
    textBox2.Clear();
    textBox3.Clear();
}

private void btnSave_Click(object sender, EventArgs e)
{
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

    DataRow drow = ds1.Tables["MyWorkers1"].NewRow();

    drow[0] = textBox1.Text;
    drow[1] = textBox2.Text;
    drow[2] = textBox3.Text;

    ds1.Tables["MyWorkers1"].Rows.Add(drow);

    con.Open();
    da.Update(ds1, "MyWorkers1");
    con.Close();

    MaxRows = MaxRows + 1;
    inc = MaxRows - 1;

    MessageBox.Show("Record / Entry Added");
}

private void btnUpdate_Click(object sender, EventArgs e)
{
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

    DataRow dRow2 = ds1.Tables["MyWorkers1"].Rows[inc];

    dRow2[0] = textBox1.Text;
    dRow2[1] = textBox2.Text;
    dRow2[2] = textBox3.Text;

    da.Update(ds1, "MyWorkers1");
    MessageBox.Show("Data Updated");
}


Drag and drop the radio buttons:

if (radiobutton1.checked==true)
    drow[3]="male";
else if(radiobutton2.checked==true)
    drow[3]="female";


Here's something I did before:

I have two radio buttons with text of M and F, for Male and Female.

public string GetSex()
{
    return radioM.Checked ? radioM.Text : radioF.Text;
}

This is called a ternary operator.

Read this thread for lots of good information from very smart people.


cmd.CommandText = " INSERT INTO SISWA(NIS,NAMA,JENISKELAMIN,ALAMAT,DATE) " +
                  " VALUES('" + nis + "','" + nama + "',+ getSex() +,'" +
                                alamat + "',getDate())";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜