|
-
Feb 6th, 2004, 10:33 AM
#1
Thread Starter
Addicted Member
.net Beginner Help Needed
Can anyone please help me.
I am new to asp.net. I have been trying for 3 days now to do what i thought is the simplest thing. I am trying to simply create a web page with a datagrid displaying information from one of my sql server tables. this is the first asp.net project i am doing. I followed a walkthrough and i keep getting this error:
Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection
Please can anyone lead me in some direction...
any help would be greatly appreciated.
Thanks.
-
Feb 6th, 2004, 11:57 AM
#2
Hyperactive Member
Try this:
Code:
<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(s as Object, e as EventArgs)
Dim strCON, strSQL as String
strCon = ("Data Source = ServerName; " & _
"initial catalog = DatabaseName; " & _
"user ID = UserName; " & _
"password = Password")
strSQL = ("Select * From myTableName")
Dim da As New SqlDataAdapter(strSQL, strCon)
Dim ds As New DataSet()
da.Fill(ds, "myData")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "myData"
DataGrid1.DataBind()
End Sub
</script>
<html>
<head>
<title>SQL Demo 2</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</html>
All you have to do is put your servername, databasename, username and password in place of the place holders I used.
It is not recomended that you put your database connection info in the page as I did above because it is not very secure so you may want to just use the code I posted for testing puposes and then set your connection string more sequrely later. I think it is safer to put it in the web config file.
-
Feb 9th, 2004, 04:38 PM
#3
Thread Starter
Addicted Member
Thanks loads. it works great.
Now going further, how do i put that code into the vb?
where do i put the import statements.
i know these are detailed questions so maybe you can lead me where to learn more about these things, i need sample code...
-
Feb 9th, 2004, 04:59 PM
#4
Hyperactive Member
Shevy if you mean the Code Behind when you say in the VB you should be able to click on the page you are working on (after you remove all the imports and script from the above sample) and choose View Code. The Page_Load Event should already be open and ready for you to put in your VB Code. You will want to do something like this:
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strCON, strSQL As String
strCON = ("Data Source = ServerName; " & _
"initial catalog = DatabaseName; " & _
"user ID = UserName; " & _
"password = Password")
strSQL = ("Select * From myTableName")
Dim da As New SqlDataAdapter(strSQL, strCON)
Dim ds As New DataSet
da.Fill(ds, "myData")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "myData"
DataGrid1.DataBind()
End Sub
As far as a good source for learning more about this topic, I have found these two beginners books to be a great place to start when learning the basics of ASP.Net and ADO.Net. The two books are "Sam's Teach Yourself ASP.Net in 21 Days" (for Access databases) and "ASP Unleashed" (for SQL databases)
-
Feb 10th, 2004, 09:59 AM
#5
Thread Starter
Addicted Member
thanks, I did what you said but now i am getting an error when i compile "type sqldataadapter not defined".
what am i doing wrong?
-
Feb 10th, 2004, 10:07 AM
#6
Hyperactive Member
oops I forgot to tell you to put this at the top of the code behind
Imports System.Data
Imports System.Data.SqlClient
-
Feb 10th, 2004, 02:43 PM
#7
Thread Starter
Addicted Member
Thanks,
I got it working.
I added capabilities to edit.
I am having trouble with the update method.
would you have a code snippet which coresponds to the page_load which you sent me?
thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|