Reputation: 2051
I'm using Web Developer 2010, ASP.NET with the VB.NET programming language and a MS-Access database (.mdb).
What I want to do is get all the data from a table called User (i.e. all the columns) where Username = ____, and then put the values in from the fields into text boxes so they can be edited. This is to make a 'My Account' page so that a user viewing the website can change his address, password etc.
I'm stuck at getting the data from the fields for each specific username.
What I've done so far is this, but doesn't work:
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data
Partial Class Account
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim tUsername As String = Session("Username")
Dim tPassword As String = Session("Password")
Dim conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Email], [CustomerName], [CustomerSurname], [Address], [Country], [TelNo], [Username], [Password] FROM [User] WHERE Username=?", conn)
DataSet(dst = New DataSet)
cmd.Parameters.AddWithValue("@Username", tUsername)
conn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader
If (reader.Read()) Then
lblName.Text = reader.GetValue(4).ToString
lblSurname.Text = reader.GetValue(5).ToString
lblUsername.Text = reader.GetValue(9).ToString
txtAddress.Text = reader.GetValue(6).ToString
txtCountry.Text = reader.GetValue(7).ToString
txtEmail.Text = reader.GetValue(2).ToString
conn.Close()
End If
End Sub
End Class
Error message:
Parameter ?_1 has no default value.
at
Dim reader As OleDbDataReader = cmd.ExecuteReader
Any help ?
Upvotes: 0
Views: 7184
Reputation: 63105
you need to give parameter as @Username
not like ?
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Email], [CustomerName], [CustomerSurname], [Address], [Country], [TelNo], [Username], [Password] FROM [User] WHERE Username=@Username", conn)
Use Using
statement when you deal with connection as below
Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
+ reader.GetString(1))
End While
' always call Close when done reading.
reader.Close()
End Using
And Also make sure reader.GetValue(Index)
index always lower than select column count.
Upvotes: 3
Reputation: 10880
Your index numbers are not lining up with your query statement.
Try
lblName.Text = reader.GetValue(2).ToString
lblSurname.Text = reader.GetValue(3).ToString
lblUsername.Text = reader.GetValue(7).ToString
txtAddress.Text = reader.GetValue(4).ToString
txtCountry.Text = reader.GetValue(5).ToString
txtEmail.Text = reader.GetValue(0).ToString
Upvotes: 0