Hi guys, I'm new here and in dire need of help for a problem that I've worked on hours.

I created a table 'tblProductCat' in the webform and wanted to populate the table with the code below:

Imports System.Data
Imports System.Data.SqlClient


Partial Class ProductCategories
    Inherits System.Web.UI.Page

    'Define DA and DS here
    Dim dataAdapter As New SqlDataAdapter
    Dim dataSet As New DataSet


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

        Dim pointer As Integer = 0

        Dim ConnectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FitXdb.mdf;Integrated Security=True;User Instance=True"
        Dim connection As New SqlConnection(ConnectionString)

        connection.Open()

        Try
            'Assign table to dataAdapter at the start of 'Try'
            dataAdapter = New SqlDataAdapter("SELECT * FROM CATEGORY", connection)
            dataAdapter.Fill(dataSet, "Category")

            If dataSet.Tables("Category").Rows.Count <> 0 Then

                While pointer <= dataSet.Tables("Category").Rows.Count
                    Dim tr As New TableRow()
                    Dim tr2 As New TableRow()
                    Dim tr3 As New TableRow()
                    Dim tr4 As New TableRow()
                    'Create new tablerows

                    Dim tc As New TableCell()
                    Dim tc2 As New TableCell()
                    Dim tc3 As New TableCell()
                    Dim tc4 As New TableCell()
                    'Create new tablecells

                    Dim img As New Image()
                    Dim link As New HyperLink()
                    Dim label As New Label()

                    img.ImageUrl = dataSet.Tables("Category").Rows(pointer).Item(3)
                    tc.Controls.Add(img)
                    tr.Controls.Add(tc)
                    'Add first row and first cell - image

                    link.Text = dataSet.Tables("Category").Rows(pointer).Item(1)
                    link.Style("Color") = "#99CCFF"
                    link.NavigateUrl = "~\ProductList.aspx?CategoryID=" & dataSet.Tables("Category").Rows(pointer).Item(0) & "&CategoryName=" & dataSet.Tables("Category").Rows(pointer).Item(1)
                    'send categoryID and categoryName as query strings to productList page
                    tc2.Controls.Add(link)
                    tr2.Controls.Add(tc2)
                    'Add second row and second cell - category link

                    label.Text = dataSet.Tables("Category").Rows(pointer).Item(2)
                    label.Style("Size") = "small"
                    tc3.Controls.Add(label)
                    tr3.Controls.Add(tc3)
                    'Add third row & third cell - category description

                    tc4.Text = "<br />"
                    tr4.Controls.Add(tc4)
                    'Add fourth row & fourth cell - add breakpoint

                    tblProductCat.Rows.Add(tr)
                    tblProductCat.Rows.Add(tr2)
                    tblProductCat.Rows.Add(tr3)
                    tblProductCat.Rows.Add(tr4)


                    pointer = pointer + 1

                End While

            End If



        Catch ex As Exception
            MsgBox("Failed to connect to database", MsgBoxStyle.OkOnly)
        End Try

        connection.Close()

    End Sub

End Class

Here's the web.config file:

<connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FitXdb.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

The problem is I can't connect to the database. Yes, the database is there, and I've checked on the naming as well. I have no earthly idea what went wrong. At one point, the error message (in a long yellow box) that goes along the line of 'offline' and 'check your root directory for *something*' during browsing appeared.

I tried creating a gridview and ran it through a data source with the same connection string and it got populated.

Anyone knows what went wrong here?

Recommended Answers

All 2 Replies

As u already set that in web config file.u need to set only
dim con As string;
con=CongigurationManager.ConnectionStrings("ConnectionString").ConnectionString

dim connections as new sqlConnection(con)
connections.open()

Actually I tried your method but that didn't work too. I then checked my database and realised that the image column does not have any value. I included the link to the image files and it worked.

Thanks anyway, really appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.