Resolved SqlException: 'Invalid column name 'id'.

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();
        }
 
Sounds more like a SQL problem rather than a C# problem. It looks like your Staff table doesn't have a column named "id".
 
Firstly, Its a terrible idea to dump code into the load event, especially if that load event is responsible for executing communication with a database.

Secondly, are you sure the column exists? And what is the relation between the above code and the "Add staff" option. I'm not seeing it?

Thirdly, it seems like it might be a Inconsistent pluralization issue.
 
Back
Top Bottom