简体   繁体   中英

Regarding error An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

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();
    }
}

Your cmd command is not correct.

change

SqlCommand cmd = new SqlCommand("select * from demo, con");

To

SqlCommand cmd = new SqlCommand("select * from demo", con);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM