mardi 31 mars 2015

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll in Winform C#

Can Anyone help me? I'm using SQL2014 and my code as below:



void loadProductsInfor()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"server=
.\SQLEXPRESS;database=CNET_Block3;
uid=sa;pwd=123456;integrated security=true";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Products";
**SqlDataReader dr = cmd.ExecuteReader(); (Exception appears at this line)**
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(),
dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[7].ToString(), dr[8].ToString(),
dr[9].ToString(), dr[10].ToString());
}
dr.Close();
con.Close();
}


I tried many things but they didn't work. I have a practical exam in the next 2 days. Please give me a hand.


Aucun commentaire:

Enregistrer un commentaire