Solved

Simple VB.net passing data from .vb file to .aspx file

Posted on 2008-06-18
7
377 Views
Last Modified: 2013-11-26
I am looking to select data from a database in the .vb file so I can do an if/then statement. I then want that information to display on the .aspx page, in a gridview, or however it would be easiest. I am new to VB.net and just now getting a handle on it.

Below is the code for both the .vb file and the .aspx file. I think I have the connection string setup correctly but im not sure how to do the if/then in the .vb file. additionally I am stumped on how to get this to talk to the .aspx page and display the data.

Thanks for your help
---.vb file---
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim now = Date.Now
        Dim Conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
        Dim SQL As String
        SQL = "SELECT * FROM court_gbs"
        Dim Comm As New SqlCommand(SQL, Conn)
        If court_gbs.judge = hoover Then
            Response.Write("test value")
        End If
        Conn.Close()
    End Sub
End Class
 
 
 
---.aspx file---
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:drocco21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 1

Author Comment

by:drocco21
ID: 21812089
Here is the error, and also court_gbs is my table name, and judge is a column

Compiler Error Message: BC30451: Name 'court_gbs' is not declared.

Source Error:

 

Line 9:          SQL = "SELECT * FROM court_gbs"
Line 10:         Dim Comm As New SqlCommand(SQL, Conn)
Line 11:         If court_gbs.judge = hoover Then
Line 12:             Response.Write("test value")
Line 13:         End If
 
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 21812147
It looks like you're missing a few pieces. First is how you get the data; ADO .NET uses a disconnected data model.

Dim dtJudges as New DataTable("Judges")
Using conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
  Using cmd As New SqlCommand("SELECT * FROM court_gbs", conn)
    Using da As New SqlDataAdapter(cmd)
      da.Fill(dtJudges)
    End Using
  End Using
End Using
 
Now, you need to bind the datatable to the grid.
GridView1.DataSource = dtJudges

It's been a while since I've used ASP.NET, do I don't recall... you might have to add
GridView1.DataBind



0
 
LVL 1

Author Comment

by:drocco21
ID: 21812715
Hello, I did what you stated above, but it is now saying the datatable is not defined. Below is the error and the new code:

Compiler Error Message: BC30002: Type 'DataTable' is not defined.

Source Error:

 

Line 4:      
Line 5:      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Line 6:          Dim dtJudges As New DataTable("Judges")
Line 7:          Using conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
Line 8:              Using cmd As New SqlCommand("SELECT * FROM court_gbs", conn)
 

Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtJudges As New DataTable("Judges")
        Using conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
            Using cmd As New SqlCommand("SELECT * FROM court_gbs", conn)
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(dtJudges)
                End Using
            End Using
        End Using
        GridView1.DataSource = dtJudges
    End Sub
End Class

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:drocco21
ID: 21812781
OK, I was playing with this and in VS it recommended adding the following:
Imports System.Data
It now is working without error, but is a blank page with no data, not sure what that is all about. If this imports System.Data is wrong feel free to yell lol
Imports System.Data.SqlClient
Imports System.Data
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtJudges As New DataTable("Judges")
        Using conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
            Using cmd As New SqlCommand("SELECT * FROM court_gbs", conn)
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(dtJudges)
                End Using
            End Using
        End Using
        GridView1.DataSource = dtJudges
    End Sub
End Class

Open in new window

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 21813291
You'll also need to import System.Data.SqlClient.

I'm assuming that you're using SQL Server as your back end database.

Did you try adding GridView1.DataBind?
0
 
LVL 1

Author Comment

by:drocco21
ID: 21813336
Sorry, should have paid more attention to what you said the first time, about the databind. I added the databind and all is well, thanks so much for your help. I have one more question and will put a link on the bottom of this, relating to if/then, then I am done. Below is the working code:
Imports System.Data.SqlClient
Imports System.Data
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtJudges As New DataTable("Judges")
        Using conn As New SqlConnection("Initial Catalog=court_dms;Data Source=DINO;Integrated Security=no; User ID=norton;PWD=goldcat13")
            Using cmd As New SqlCommand("SELECT * FROM court_gbs", conn)
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(dtJudges)
                End Using
            End Using
        End Using
        GridView1.DataSource = dtJudges
        GridView1.DataBind()
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:drocco21
ID: 31468305
Thanks
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I add google maps in vb.net. 2 49
Set arraylist as datatable 2 29
reading an excel file vb.net 2 47
Cannot interpret token '"' at position 10. 3 25
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question