Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to connect to mssql server from many days and i searched in google also many times but continuously i am failing to connect to sql server using connection string.
When i connect using ms sql server studio then it works fine but when i connect to mssql server using connection string then it fails to connect. Website where i am registerd provided me this connection string

"workstation id=chattha.mssql.somee.com;packet size=4096;user id=rizwan;pwd=censored;data source=chattha.mssql.somee.com;persist security info=False;initial catalog=chattha".

I tried these connection strings in my code
C#
con.Open( _
        "Provider = sqloledb;" & _
        "Data Source=mssql.chattha.somee.com;" & _
        "Initial Catalog=chattha;" & _
        "User ID=rizwan;" & _
         "Password=censored;")

I also tried this connection string also
C#
con.Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=rizwan;Password=censored;Initial Catalog=chattha;Data Source=chattha.mssql.somee.com")

Above all i tried but any of them is not working.
I tried below code also but it also did not worked.

Dim dbconn, sql, dbcomm, dbread
            'dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("AddressBook.mdb"))
            'dbconn = New OleDbConnection("Provider = sqloledb;user id=rizwan;pwd=censored;data source=chattha.mssql.somee.com;persist security info=False;initial catalog=chattha")
            dbconn = New OleDbConnection("Data Source=chattha.mssql.somee.com ;Initial Catalog='chattha';USE='rizwan';PWD='censored'")
            dbconn.Open()
        sql = "SELECT * FROM Table1"
        dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
        Table1.DataSource = dbread
        Table1.DataBind()
        dbread.Close()
        dbconn.Close()


For experiment i added gridview and set sqlDataSource and it works fine and show records in gridview correctly. But i am failing to use connection string in code.
Note: I added this reference and working with it "Microsoft ActiveX Data Objects Library 6.1 Library.
Please help me.
Posted
Updated 15-Jul-15 20:50pm
v3
Comments
Akhil Mittal 16-Jul-15 2:24am    
What error are you getting?
[no name] 16-Jul-15 2:52am    
I am getting this error "[DBNETLIB][ConnectionOpen (Invalid Instance()).]Invalid connection."
_Asif_ 16-Jul-15 3:03am    
Are you able to do this from command line?
Telnet chattha.mssql.somee.com 1433
[no name] 16-Jul-15 5:06am    
In command prompt i typed Telnet chattha.mssql.somee.com 1433 and it said telnet is not recognized as internal or external command.
[no name] 16-Jul-15 4:09am    
In command prompt i typed Telnet chattha.mssql.somee.com 1433 and it said telnet is not recognized as internal or external command.

1 solution

Try explicitely defining the port
C#
con.Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=rizwan;Password=censored;Initial Catalog=chattha;Data Source=chattha.mssql.somee.com,1433")

Also ensure that communication using the correct port is allowed in the firewall.
 
Share this answer
 
v2
Comments
[no name] 16-Jul-15 4:51am    
Thanks for helping your provided connection string is working. Please explain why we need to write port in connection string while port was not blocked by firewall. And also explain some other details related to it.
Wendelius 16-Jul-15 5:09am    
You probably encountered this[^]
[no name] 16-Jul-15 5:27am    
Thanks for providing information.
[no name] 16-Jul-15 4:55am    
My firewall was already off.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900