Solved

confused on how to use data with labels in aspx via vb

Posted on 2015-02-21
7
136 Views
Last Modified: 2015-02-22
it has been a good while since I worked with visual studios for web. Can someone help me with the following please?

I have a main page which we will call main.aspx

I have some labels that I want to dynamically change based on data in a simple sql database.

mainly a persons name, date, and link to more info

I'm at a complete lost:
I have made the database in sql express on the local machine and populated the table called table. the database is called database1

I added to the web.config file the following:
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <connectionStrings>
    <add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated
     Security=SSPI;AttachDBFilename=|DataDirectory|\database1.mdf;User Instance=true"
     providerName="System.Data.SqlClient"/>
  </connectionStrings>
    
</configuration>

Open in new window


then in the main.aspx.vb
i'm lost as to how to access the data (bind it) to the control

I have something like this so far but i'm certain it is not right.  Please help..
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <connectionStrings>
    <add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated
     Security=SSPI;AttachDBFilename=|DataDirectory|\database1.mdf;User Instance=true"
     providerName="System.Data.SqlClient"/>
  </connectionStrings>
    
</configuration>

an example would be wonderful. thanks!

Open in new window

0
Comment
Question by:bbimis
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:BullfrogSoftware
ID: 40623794
Imports System.Data.SqlClient

Partial Class main
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim ConnectionString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("TESTConnectionString").ToString()
        Dim Id As String = "1"

        Dim MyConn As New System.Data.SqlClient.SqlConnection(ConnectionString)
        Dim MyComm As New System.Data.SqlClient.SqlCommand("SELECT * FROM [dbo].[Table] WHERE id = " + Id, MyConn)

        MyConn.Open()
        Dim DataReader As System.Data.SqlClient.SqlDataReader = MyComm.ExecuteReader()

        While (DataReader.Read())

            Label1.Text = DataReader("name")
            Label2.Text = DataReader("date")
            Label3.Text = DataReader("link")

        End While

        DataReader.Close()
        MyConn.Close()


    End Sub
End Class

Open in new window

0
 

Author Comment

by:bbimis
ID: 40623845
Okay trying to follow you but I get the following error as shown in picture.
here is the code I'm using in the vb part
Imports System.Data.SqlClient

Partial Class main
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim ConnectionString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\tls\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\App_Data\Database1.mdf";Integrated Security=True").ToString()
        Dim Id As String = "1"

        Dim MyConn As New System.Data.SqlClient.SqlConnection(ConnectionString)
        Dim MyComm As New System.Data.SqlClient.SqlCommand("SELECT * FROM [dbo].[Table] WHERE id = " + Id, MyConn)

        MyConn.Open()
        Dim DataReader As System.Data.SqlClient.SqlDataReader = MyComm.ExecuteReader()

        While (DataReader.Read())

            Label1.Text = DataReader("date")
            

        End While

        DataReader.Close()
        MyConn.Close()


    End Sub
End Class[embed=file 899666]

Open in new window

error1.PNG
0
 
LVL 1

Expert Comment

by:BullfrogSoftware
ID: 40624276
If you are going to specify your own connection string, instead of using the web.config, then the connection string line needs to be like this:

Dim ConnectionString As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\tls\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\App_Data\Database1.mdf"";Integrated Security=True"

Open in new window


Please note the double double-quotes inside the string value.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:bbimis
ID: 40624321
So in the Web.config would I add that whole string to the connection string.  I guess that's where I'm having more trouble.  Of what should actually be in the Web.config.  Cause I'd like to read from there.
0
 
LVL 1

Accepted Solution

by:
BullfrogSoftware earned 500 total points
ID: 40624326
Ok, if you want to use the web.config, go back to the original code I sent and replace TESTConnectionString with ApplicationServices (that is what you have the connection named in your web.config)
0
 

Author Comment

by:bbimis
ID: 40624327
Aww OK. Thanks will try.
0
 

Author Closing Comment

by:bbimis
ID: 40624346
thank you so much that is what I needed!

I'm sure I will have more questions along the way. good to know I have help out there.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Turn on intranet settings 1 36
Get size of each directory on each mapped drive 5 20
Get the selected ValueMember of Combobox 5 19
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now