Search code examples
c#sql.netlocaldb

i tried several techniques but failed to handle this error System.Data.SqlClient.SqlException' occurred in System.Data.dll


I have the following error occured when try to execute my program, do not know how to handle, following is the code , and i also attached the screenshots. i am new to .net and sql. Thanks

following is the screenshot of error generate

    public partial class adminInfo : Form
    {
        SqlCommandBuilder scb;
        public adminInfo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PMS obj = new PMS();
            obj.Show();
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            adminAdd_delete obj = new adminAdd_delete();
            obj.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ddd\Desktop\Pharmacy managment system\Pharmacy managment system\adminInfo.mdf;Integrated Security=True;Connect Timeout=30");
            SqlDataAdapter sda = new SqlDataAdapter(@"SELECT        CNIC, [JOB TITLE], SALARY, NAME
FROM            admininfo", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
           dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

Solution

  • Seems you're new comer to stackoverflow, I simply suggest to define the DataTable how it looks like,

    DataTable dt= new DataTable("admininfo");  
    

    Now, the structure of DataTable is same as admininfo table in SQL database.

    You just made a instance of DataTable but you need to set columns manually which belong to the DataTable or automatic way like above.

    Hope this helps..