helo guys, i have a little problem here. i've always been using MS access as database to connect my vb.net applications. Now i've design a webpage in asp.net and i want to connect the login.aspx page with the sqlexpress provided in vb.net framework..can anyone help me with the steps?? just for test purpose i wrote the following code

Imports System.Data.SqlClient
Imports System.Configuration

Partial Class login
    Inherits System.Web.UI.Page

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim sqlCon As New SqlConnection("Data Source=alvin;Initial Catalog=Database.mdf")
        sqlCon.Open()
        MsgBox(sqlCon.State.ToString)
        sqlCon.Close()

    End Sub
End Class

Dani AI

Generated

Brief summary and practical follow-ups based on the thread: correctly pointed out that System.Data.OleDb is the right route for Access databases, while ’s AttachDbFilename approach is a common, working pattern when an .mdf lives under App_Data. A few practical best practices, a safe example pattern, and troubleshooting pointers follow.

Store the connection string in web.config and open connections with a Using block to ensure proper disposal. Use parameterized queries for login checks (never concatenate user input). A minimal pattern (replace names as needed):

<!-- web.config -->
<connectionStrings>
  <add name="MyConn"
       connectionString="Server=.\SQLEXPRESS;Database=MyAppDb;Integrated Security=True;"
       providerName="System.Data.SqlClient" />
</connectionStrings>
' server-side (VB.NET)
Dim cs = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Using cn As New SqlConnection(cs)
    cn.Open()
    Dim cmd As New SqlCommand("SELECT COUNT(1) FROM Users WHERE Username=@u AND PasswordHash=@p", cn)
    cmd.Parameters.AddWithValue("@u", txtUser.Text)
    cmd.Parameters.AddWithValue("@p", computedHash)
    Dim authenticated = Convert.ToInt32(cmd.ExecuteScalar()) > 0
End Using

Troubleshooting & cautions:

  • MsgBox on the server side does not show in the browser; use a Label, Response.Write, or server logs for feedback.
  • If an .mdf is placed in App_Data, check file permissions for the app pool identity and avoid "Copy always" (which overwrites the .mdf on each debug run).
  • Confirm the SQL Express service and correct instance name (.\SQLEXPRESS or a named instance). For production, prefer a real SQL Server or LocalDB (LocalDB is the modern lightweight option).
  • For Access: Jet (Microsoft.Jet.OLEDB.4.0) is 32-bit only; on 64-bit hosts use the ACE provider and ensure the Access Database Engine is installed.
  • Never store plaintext passwords; store salted hashes and use a secure hashing algorithm.

For modern guidance on LocalDB and SQL Express usage see Microsoft Docs (search "SQL Server Express LocalDB").

Recommended Answers

All 3 Replies

Connect to an Access database using sqlclient is not possible. System.Data.OleDb is one of the best solution for any non-SQL Server database system.

EXAMPLE CODE on USING OLEDB IN VB.NET:

Imports System.Data.OleDb
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader

/* use in page load event */
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=C:\emp.mdb;")
'provider to be used when working with access database
cn.Open()
cmd = New OleDbCommand("select * from table1", cn)
dr = cmd.ExecuteReader
While dr.Read()
TextBox1.Text = dr(0)
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
' loading data into TextBoxes by column index
End While

If you have created a new DB in SQL Express and the table is in the 'app_data' folder in your project then something like this could work:

Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
        Dim sql As String = "SELECT * FROM tablename"
        Dim cmd As New SqlCommand(sql, conn)
        conn.Open()

you can then use this to fill a dataset or table in the same way you would have before

If you have created a new DB in SQL Express and the table is in the 'app_data' folder in your project then something like this could work:

Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
        Dim sql As String = "SELECT * FROM tablename"
        Dim cmd As New SqlCommand(sql, conn)
        conn.Open()

you can then use this to fill a dataset or table in the same way you would have before

Thx loads friend..it works great..am gonna work on it..thx again

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.