I have two table the first table name is tbl_Account have a fieldName UserID, Username, Password and the second table name is tbl_Item have a fieldname ItemID, ItemName, UserID. How can I get the value of UserID in tbl_Account and put it to tbl_Item row UserID? Defends to the user who login.
public void InsertRecord()
{
Connection connection = new Connection();
try
{
string sql = "INSERT INTO tbl_Item VALUES (@itemId, @itemName, @logId)";
MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@itemId", GenerateID());
cmd.Parameters.AddWithValue("@itemName", ItemName);
cmd.Parameters.AddWithValue("@logId", GenerateIDs());
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Update Successfully", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show("An error occurred: " + e, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//my code for btnAdd
private void button1_Click(object sender, EventArgs e)
{
Item item = new Item();
item.ItemID = item.GenerateID();
item.ItemName = textBox2.Text;
item.Account.UserID = item.Account.GenerateID();
item.InsertRecord();
}