Results 1 to 7 of 7

Thread: .net Beginner Help Needed

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    .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.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232
    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...

  4. #4
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, _
    2.         ByVal e As System.EventArgs) Handles MyBase.Load
    3.  
    4.         Dim strCON, strSQL As String
    5.  
    6.         strCON = ("Data Source = ServerName; " & _
    7.            "initial catalog = DatabaseName; " & _
    8.            "user ID = UserName; " & _
    9.            "password = Password")
    10.  
    11.         strSQL = ("Select * From myTableName")
    12.  
    13.         Dim da As New SqlDataAdapter(strSQL, strCON)
    14.         Dim ds As New DataSet
    15.         da.Fill(ds, "myData")
    16.         DataGrid1.DataSource = ds
    17.         DataGrid1.DataMember = "myData"
    18.         DataGrid1.DataBind()
    19.  
    20.     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)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232
    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?

  6. #6
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    oops I forgot to tell you to put this at the top of the code behind

    Imports System.Data
    Imports System.Data.SqlClient

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232
    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
  •  



Click Here to Expand Forum to Full Width