SqlCommand issue

Peter_1985 2,486 Reputation points
2023-01-04T06:19:52.873+00:00

Hi all,
There is the proper existing connection on Webconfig file. What is the reason to the issue below?
275895-image.png

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
0 comments No comments
{count} votes

Accepted answer
  1. Albert Kallal 4,651 Reputation points
    2023-01-04T06:51:52.147+00:00

    Hum, have you tested the connection?

    I suggest you consider letting visual studio "build" the connection for you.

    If this is a webforms project, then you can try:

    project->"my project name" properties (usually the last choice).

    275849-image.png

    And then use "settings".

    275951-image.png

    If you click on the [...], then the connection builder is launched.

    We are assuming that you have say SQL server express up and running.

    But, using the connection builder can help, and it does have a "test" conneciton.

    So, after clicking on [...], then we get this:

    275934-image.png

    So, above is nice, since then it will "create/put" the connection you create into web config for you.

    and with the test connection button then you can test/try the database connection without having to run code just yet.

    so, for example, in above I created a few connections.

    And, in code, I can thus now say do this:

    void LoadGrid()  
    {  
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))  
        {  
            string strSQL =   
                @"SELECT * FROM tblHotelsA ORDER BY HotelName";  
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))  
            {  
                conn.Open();  
                DataTable rstData = new DataTable();  
                rstData.Load(cmdSQL.ExecuteReader());  
                GridView1.DataSource = rstData;  
                GridView1.DataBind();  
            }  
        }  
    }  
    

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta, Canada

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 40,741 Reputation points
    2023-01-04T07:04:50.613+00:00

    There is the proper existing connection on Webconfig file.

    Not by the error message, that says wrong server name or server not reachable; check that first.
    In dev enviroment a local server is oft used, which may not exists in prod enviroment.

    0 comments No comments