Friday, March 9, 2012

Remote MSSQL2000 server connection with .net

I have a problem in connecting to my hosting database using .net file.
I am able connect to database with my ASP page.

Any sugession is greately appretiated!

Here is my code:

".net" page:


<%@. page language='vb' debug='true' %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<html>
<script runat="server"
Sub Page_Load(Src As Object, E As EventArgs)
Dim MyConnection As SqlConnection
Dim da AS sqlDataAdaptor
Dim ds As DataSet
MyConnection = New SqlConnection("DSN=***;UID=***;PWD=***")
da = new sqlDataAdaptor("select * from city", MyConnection)
Try
MyConnection.open()
Response.write "Connection Success " & MyConnection.Database & "<br>"
catch sx AS sqlException
Response.write "Connection successful: <br>"
End Try

ds=new Dataset()
da.Fill(ds, "City")

With DataGrid1
.DataSource = ds.Tables("city").DefaultView
.DataBind()
End With

MyConnection.Close()

End Sub

</script
<body
<p>
<ASP:dataGrid id='DataGrid1' runat='server'/>
<p>
</body>
</html>
--------------


"ASP" page:

Set DB = Server.CreateObject("ADODB.Connection")
DB.Open ConnectionString

SET RSCheckeMail = DB.Execute ("select * from city")

While not RSCheckeMail.eof
Response.write "City: " & RSCheckeMail("Name") & "<br>"
RSCheckeMail.Movenext
Wend


Thanks for your help in advance.Please any help on this?|||As far as I understand it, in order to use a DSN in the connection string you have to use the ODBC provider, which is not recommended. You really need to try to use SQLClient.

Please check out the connection string suggestions athttp://www.connectionstrings.com. I would think that you would need to connect using an IP address.

Terri|||tmorton,
Thank you for your suggestion. I am trying with SQLClient only or I am missing some thing here.
I tried with all the connection strings explained in the URL: http://www.connectionstrings.com. No luck.

Any more suggestions? please.

KP|||can you post the updated code ?|||Yes, you are using SQLClient, which is good. What I was trying to say but wasn't clear is that if you want to use a DSN you would need to use ODBC instead.

Did you try a connection string in this format?
"Data Source=190.190.200.100;Network Library=DBMSSOCN;Initial Catalog=yourDatabase;User ID=yourSQLUserID;Password=youSQLUserPassword;"

If you could post your attempted connection string and any errors your are receiving this would help to troubleshoot the problem.

Terri|||tmorton,
Thank you for your advice.

I got the solution for my problem.

I think all new .net learners like me has this problem initially. I mistake I did was, not using VS .net to start my development.
All beginners you need to remember to do this when you start for the first time when you start running some sample .net pages: Create an application (vb or c#) depends as per your preference. Then add all the pages to that Application. This will make your test pages will run properly (you won't waste time like me for 3 days to scratch your head to run a simple page!!).

Here is my final code to connect remote server from your IIS:


<%@. page language='vb' %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %
<script runat="server"
Sub Page_Load(Src As Object, E As EventArgs)
Dim MyConnection As SqlConnection
Dim da AS sqlDataAdapter
Dim ds As DataSet
MyConnection = new SqlConnection("server=65.49.254.24;database=aspdotnet;uid=aspdotnetuser;pwd=aspdotnetpassword")

da = new sqlDataAdapter("select * from city", MyConnection)
Try
MyConnection.open()
Response.write ("Connection Success <br>")
catch sx AS sqlException
Response.write ("Connection failed: <br>")
End Try

ds=new Dataset()
da.fill(ds, "cities")

With DataGrid1
.DataSource = ds.Tables("cities").DefaultView
.DataBind()
End With
MyConnection.Close()
End Sub

</script>
<html>
<body
<p>
<ASP:dataGrid id='DataGrid1' runat='server'/>
<p>
</body>
</html>

Please correct me if you can improve the connection.

Thanks for your support.

KP|||this is good...works..but if you just want to display a set of results...you can use executereader (for returning multiple columns) which is faster than datasets...am sure you will learn along the journey...

in your case you could have just created an sqlcommand...xample..( off the top of my head)


dim mysqlcommand as sqlcommand
Dim conn As SqlConnection
dim strsql as string
conn="..."
strsql ="Select * from table"
mysqlcommand=new sqlcommand(strsql,conn)
conn.open()
datagrid.datasource=mysqlcommand.executereader()
conn.close()

also remember to open the connection late and close early...

hth|||Dinakar,
Thanks for your suggestions. I will remember them.
You are right! Just now I started long journey...

KP

No comments:

Post a Comment