Discussion sql server 2005 connection string to .net application

  • Monday, November 28, 2011 9:12 AM
     
      Has Code

    hi

    i am using sqlserver 2005 express edition and visual studio 2010 .i am not able to  connect  sql server database to the .net application.Can anyone plz tell me how to do it.i wrote the below code.but its not working.the problem is with establishing the connection.pls let me know the correct connection string..

     

     

        protected void Button1_Click(object sender, EventArgs e)

        {

    SqlConnection con=new SqlConnection("server=JKC-PC/SQLEXPRESS;database=exam;Trusted_Connection=True;");

            if (con.State == ConnectionState.Closed)

            {

                con.Open();

            }

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb");

            Label2.Text = cmd.ExecuteScalar().ToString();

     

    }


    • Edited by sql learner12 Monday, November 28, 2011 9:20 AM
    • Moved by Bob BeaucheminMVP Monday, November 28, 2011 4:51 PM Moved to the appropriate forum for best response (From:.NET Framework inside SQL Server)
    • Changed Type sql learner12 Thursday, February 09, 2012 4:09 AM
    •  

All Replies

  • Monday, November 28, 2011 9:22 AM
     
     

    boss try sending the connection obj (con) to sqlcommandobj as

     SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

    if u get any exception do post it
    • Edited by Prahalnathan Monday, November 28, 2011 9:22 AM
    •  
  • Monday, November 28, 2011 9:32 AM
     
      Has Code

    hi 

    thank you.. but i cud see its not getting connected to sql server 

            SqlConnection con = new SqlConnection("server=JAIPALMUSKU-PC/SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=schedularforexam");

     

            if (con.State == ConnectionState.Closed)

            {

                con.Open();

            }

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

            Label2.Text = cmd.ExecuteScalar().ToString();

     

    error:

    Line 26:         if (con.State == ConnectionState.Closed)
    Line 27:         {
    Line 28:             con.Open();
    Line 29:         }
    Line 30:         SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

     



  • Monday, November 28, 2011 9:35 AM
     
     

    in the connction name  DB name is missing..

    it should be something like this

     

    SqlConnection connectionString = new SqlConnection();

    connectionString.ConnectionString = "Data Source =server; Database=DB1; Integrated Security=SSPI;Pooling=no";

     

    post about how it goes and also the exception message
  • Monday, November 28, 2011 9:45 AM
     
     

    same error again..unable to open connection

     

       protected void Button1_Click(object sender, EventArgs e)

        {

            SqlConnection connectionString = new SqlConnection();

            connectionString.ConnectionString = "Data Source =JAIPALMUSKU-PC/SQLEXPRESS; Database=schedularforexam; Integrated Security=SSPI;Pooling=no";

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",connectionString);

            if (connectionString.State == ConnectionState.Closed)

            {

                connectionString.Open();

            }

     

            Label2.Text = cmd.ExecuteScalar().ToString();

     

        }

    }

     



  • Monday, November 28, 2011 9:53 AM
     
     
    is the server name and DB correct?. 
    • Edited by Prahalnathan Monday, November 28, 2011 9:54 AM
    •  
  • Monday, November 28, 2011 9:58 AM
     
     
    yes i am able to connect to the database and retrieve the data  by using the smart tag above the dropbox..i cud retrieve all the feilds from the database to the form..but not able connect it by writing the code.
  • Monday, November 28, 2011 12:13 PM
     
     
  • Wednesday, November 30, 2011 2:03 AM
    Moderator
     
     

    Hi sql learner12,

    The connection can’t be opened caused by the incorrect Connection String. You can use Server Explorer to get the correct Connection String. Just do like this:

    1.       Find out the database which you want to connect in the Server Explorer;

    2.       Double click the database, then you will find the red cross attached to the database icon has disappeared, it means you have already connected to the database;

    3.       Look at Properties Window, there’s a property named Connection String, please copy the value of the property and paste it to your code.

    Best Regards,


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
  • Thursday, February 09, 2012 3:37 AM
     
     
    thank you Allen..

    akhila