codify
Member
- Joined
- Dec 4, 2020
- Messages
- 18
- Programming Experience
- 1-3
I am making a window form database management system. When I click on the "Add staff" option, this error occurs System.Data.SqlClient.SqlException: 'Invalid column name 'id'.' on line number : 20
Code:
private void StaffInformation_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'hospitalDataSet.staff' table. You can move, or remove it, as needed.
using (SqlConnection con1 = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True"))
{
string str2 = "SELECT * FROM staff";
SqlCommand cmd2 = new SqlCommand(str2, con1);
SqlDataAdapter da = new SqlDataAdapter(cmd2);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = new BindingSource(dt, null);
}
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");
con.Open();
string str1 = "select max(id) from staff;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
textBox1.Text = "1";
}
else
{
int a;
a = Convert.ToInt32(dr[0].ToString());
a = a + 1;
textBox1.Text = a.ToString();
}
}
con.Close();
}