Hi
I'm working at an C# 2008 and MS ACCESS app and I encounter a few problems(I'm new to SQL and C#).
The first thing I wanna do is to select the last record from one table.
If I specify the record ID everything works fine but I don't know how to go to the last record.
CODE
string cl_cmd = @"select first_name,last_name,ID_Nod from cmmd WHERE ID_cmd = 5";
I tried to use IDENT_CURRENT('table') but it didn't work.
The second problem is adding the value from a check box intro a Yes/No field AND displaying the value of the Yes/No field using a check box.
I have a login table with 4 field ID, username, password and admin(Yes/No).
If I login as admin I have the possibility to change other's people right and data like password and admin rights.
The problem is how do I convert the value returned by the check box(true or false) so that I can insert it into my table when I want to update or add new users.
Here is the code for my FIND button:
CODE
private void btnFind_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < mydset.Tables[0].Rows.Count; i++)
{
if (Convert.ToInt32(mydset.Tables[0].Rows[i]["usrlg_ID"]) == Convert.ToInt32(txtID.Text))
{
txtUsrnm.Text = mydset.Tables[0].Rows[i]["username"].ToString();
txtPswd.Text = mydset.Tables[0].Rows[i]["password"].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
This works fine if I don't try to getthe value from the "admin" field and display it as a checkbox(checked if is admin unchecked if not).
Also, when I try to update the data displayed, including the checkbox value I receive an "Update statement error":
CODE
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
/*
* update process is also same since the update statement will not return any value
* we will use Command object and inside sql statement as in delete sql statement you have to
* use some variables, you have to update address and tel by using values in textboxes
* */
OleDbConnection mycon = new OleDbConnection(connectionstring);
string sql = "update login set username = '" + txtUsrnm.Text + "' , password = '" + txtPswd.Text + "' where Student_ID = " + txtID.Text +"";
OleDbCommand mycommand = new OleDbCommand(sql, mycon);
mycon.Open();
mycommand.ExecuteNonQuery();
mycon.Close();
MessageBox.Show("Record is successfully updated");
//after update fill dataset and datagrid again
string sql2 = "select * from login";
OleDbDataAdapter adap = new OleDbDataAdapter(sql2, mycon);
// DataSet should be cleared firstly
mydset.Clear();
adap.Fill(mydset);
dgvUsr.DataSource = mydset.Tables[0];
// Clear the textboxes also, since the record is updated
txtID.Text = "";
txtUsrnm.Text = "";
txtPswd.Text = "";
chkAdmin.Checked = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}