Hi, I'm very new to ASP.net. I'm attempting to write a simple web page that connects to sql server to insert a new row after getting a value entered in a textbox field. The problem is, I keep getting the error: "Object reference not set to an instance of an object.", and it points to a parameter variable in my code-behind file. If I manually set the value for the parameter (txtMemberNumber.Text) in the code-behind instead of trying to get it dynamically from the page, it works... I can't reference the value I type in on the page. What am I missing??? Thanks.
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents txtMemberNumber As System.Web.UI.WebControls.TextBox
<snipped out>
Private Sub bInsert_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles bInsert.ServerClick
Dim str As String
str = "Insert into dbo.members values (@m1)"
Dim inscom As New SqlCommand(str, SqlConnection1)
SqlDataAdapter1.InsertCommand = inscom
SqlDataAdapter1.InsertCommand.Parameters.Add(New _
SqlParameter("@m1", SqlDbType.Int, 10))
SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = _
txtMemberNumber.Text
SqlConnection1.Open()
SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
End Sub
End Class
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents txtMemberNumber As System.Web.UI.WebControls.TextBox
<snipped out>
Private Sub bInsert_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles bInsert.ServerClick
Dim str As String
str = "Insert into dbo.members values (@m1)"
Dim inscom As New SqlCommand(str, SqlConnection1)
SqlDataAdapter1.InsertCommand = inscom
SqlDataAdapter1.InsertCommand.Parameters.Add(New _
SqlParameter("@m1", SqlDbType.Int, 10))
SqlDataAdapter1.InsertCommand.Parameters("@m1").Value = _
txtMemberNumber.Text
SqlConnection1.Open()
SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
End Sub
End Class