Reputation: 3
I am beginner with c#, when i execute my code it shows an error "An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code".
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL
This is my Code :
public partial class _default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { showdata(); }
private void showdata()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("server=demo; database=demo; Integrated Security=True;");
SqlCommand cmd = new SqlCommand("select * from demo, con");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
Upvotes: 0
Views: 1463
Reputation: 3
Thank you for your help i appreciate the same. I got my answer from a friend, there was an @ missing in my query.
SqlConnection con = new SqlConnection(@"server=demo; database=demo; Integrated Security=True;");
After updating it i was able to see my data on browser.
But once again thank you for your help everyone.
Upvotes: 0
Reputation: 38
Your cmd command is not correct.
change
SqlCommand cmd = new SqlCommand("select * from demo, con");
To
SqlCommand cmd = new SqlCommand("select * from demo", con);
Upvotes: 0