I keep receiving "Object reference not set to an instance of an object" error when trying to run this code:
<%@ Page Language="
vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Sub Page_Load
'lblquestion.text="Do you want to add another employee"
username.text = "User Name:"
password.text = "Password:"
fname.text = "First Name:"
lastName.text = "Last Name:"
email.text = "Email:"
End Sub
Sub addUser(Sender As Object, E As EventArgs)
Dim connectionString As String
Dim queryString As String
Dim data As New DataSet()
Dim dbConnection As OleDbConnection
Dim dataAdapter As OleDbDataAdapter
' set the connection and query details
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Server.MapPath("db1.mdb")
queryString = "SELECT username, password, frist_name, last_name, email FROM members;"
' open the connection and set the command
dbConnection = New OledbConnection(connectionString)
dataAdapter = New OledbDataAdapter(queryString, dbConnection)
' fill the dataset with the data
dataAdapter.Fill(data, "members")
' -----------------------------------------------------------------
' add a new row to the table
Dim table As DataTable
Dim newRow As DataRow
table = data.Tables(" members")
newRow = table.NewRow()
newRow("username") = txtfname.text
newRow("password") = txtpass.text
newRow("frist_name") = textfname.text
newRow("last_name") = textlname.text
newRow("email") = textemail.text
table.Rows.Add(newRow)
' bind the data grid to the new data
DataGrid.DataSource = table
DataGrid.DataBind()
' ================================================== ===============
' generate the update commands
Dim commandBuilder As OleDbCommandBuilder
commandBuilder = New OleDbCommandBuilder(dataAdapter)
dataAdapter.InsertCommand = commandBuilder.GetInsertCommand()
' ================================================== ===============
' update the data store
dataAdapter.Update(data, "members")
'================================================= ================
End Sub
</script>
<html>
<head>
</head>
<body>
<asp:Label id="lblquestion" runat="server"></asp:Label>
<form runat="server">
<table border="0">
<tbody>
<tr>
<td>
<asp:Label id="username" runat="server"></asp:Label></td>
<td>
<asp:TextBox id="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="password" runat="server"></asp:Label></td>
<td>
<asp:TextBox id="txtpass" runat="server" textmode="password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="fname" runat="server"></asp:Label></td>
<td>
<asp:TextBox id="textfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lastName" runat="server"></asp:Label></td>
<td>
<asp:TextBox id="textlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="email" runat="server"></asp:Label></td>
<td>
<asp:TextBox id="textemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="Add" onclick="addUser" runat="server" text="Add New"></asp:button>
</td>
</tr>
</tbody>
</table>
</form>
<asp:DataGrid id="DataGrid" runat="server" visible="true"></asp:DataGrid>
</body>
</html>
thax for your help