Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please help me out to solve this SqlException "invalid object name 'employee'".

C#
<pre>using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;


namespace pharmacymanagement
{
    public partial class Employee : Form
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;
    
            public Employee()
        {
            InitializeComponent();
        }
                private void Employee_Load(object sender, EventArgs e)
        {

        }

        private void insert_Click(object sender, EventArgs e)
             {
                 con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("INSERT INTO employee(empName,age,telep,email,gender) VALUES (@empName,@age,@telep,@email,@gender)", con);
                cmd.Parameters.AddWithValue("@empName", eName.Text);
                cmd.Parameters.AddWithValue("@age", eage.Text);
                cmd.Parameters.AddWithValue("@telep", tp.Text);
                cmd.Parameters.AddWithValue("@email", eEmail.Text);
                cmd.Parameters.AddWithValue("@gender", eGender.SelectedItem.ToString());
                cmd.ExecuteNonQuery();
                
          

        }


What I have tried:

I have no idea how to solve this error.
Posted
Updated 15-Sep-21 3:00am
v2
Comments
Richard MacCutchan 15-Sep-21 4:06am    
The message is telling you that the name employee does not exist as a table in your database.

Your connection string doesn't specify a database, so SQL doesn't know which to look in for the Employee table.
Here's an example of one of mine:
Data Source=GRDESK\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True

This specifies "Testing" as the default database and my queries now access that.
 
Share this answer
 
Comments
Sandesha Senaratne 15-Sep-21 6:25am    
Thank you very much!! Problem Solved.
OriginalGriff 15-Sep-21 7:03am    
You're welcome!
Marcos Guimaraes de Godoi Junior 6-Jun-22 16:16pm    
Man you saved my life. Thank you so much!!!!!
OriginalGriff 7-Jun-22 0:41am    
You're welcome!
Denys Adamov 12-Jun-22 12:41pm    
You helped me too! Thanks <3
Either create a table called employee in the database you're connecting to, change the connection to point to a database where that table already exists, or change your query to specify the correct table name.

We can't tell you which, since we have no access to your system, and we cannot see your database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900