I'm relatively new to .NET, and I have built a calendar solution where I have just one problem left. When someone hits the submit button, here is my block of code:
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
Dim MyConnection As SqlConnection
MyConnection = New SqlConnection("server=*********;database=*******;Trusted_Connection=yes")
Dim eventMonth = Integer.Parse(eventMonth.Text)
Dim eventDay = Integer.Parse(eventDay.Text)
Dim eventYear = Integer.Parse(eventYear.Text)
Dim eventTitle = eventTitle.Text
Dim eventTime = eventTime.Text
Dim eventAddedBy = eventAddedBy.Text
Dim con As SqlConnection = New SqlConnection(Application.Contents("SQLConnect"))
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO events SET event_month = @eventMonth, event_day = @eventDay, event_year = @eventYear, event_title = @eventTitle, event_time = @eventTime, even_added_by = @eventAddedBy", con)
cmd.Parameters.Add("@eventMonth", eventMonth)
cmd.Parameters.Add("@eventDay", eventDay)
cmd.Parameters.Add("@eventYear", eventYear)
cmd.Parameters.Add("@eventTitle", eventTitle)
cmd.Parameters.Add("@eventTime", eventTime)
cmd.Parameters.Add("@eventAddedBy", eventAddedBy)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
And instead of completing the insert statement, I'm getting the following error, which I don't know how to fix:
Object variable or With block variable not set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object variable or With block variable not set.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack) +929
calendar.WebForm1.addButton_Click(Object sender, EventArgs e) in C:\Documents and Settings\Shaun Worcester\VSWebCache\131.238.126.132\worceste\calendar\WebForm1.aspx.vb:56
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
Any ideas?your code is mostly correct.
you add this lines befor exexcute
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.ExecuteReader
if you already do this then check any object are not created with new keywordYou have the INSERT statement is incorrect, try this instead:
INSERT INTO events (event_month, event_day, event_year, event_title, event_time, even_added_by) VALUES (@eventMonth, @eventDay,@eventYear,@eventTitle,@eventTime,@eventAddedBy)also you had a typo
even_added_by
should be
event_added_by
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
Dim MyConnection As SqlConnection
MyConnection = New SqlConnection("server=*********;database=*******;Trusted_Connection=yes")
Dim eventMonth = Integer.Parse(eventMonth.Text)
Dim eventDay = Integer.Parse(eventDay.Text)
Dim eventYear = Integer.Parse(eventYear.Text)
Dim eventTitle = eventTitle.Text
Dim eventTime = eventTime.Text
Dim eventAddedBy = eventAddedBy.Text
Dim con As SqlConnection = New SqlConnection(Application.Contents("SQLConnect"))
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO events SET event_month = @eventMonth, event_day = @eventDay, event_year = @eventYear, event_title = @eventTitle, event_time = @eventTime, even_added_by = @eventAddedBy", con)
cmd.Parameters.Add("@eventMonth", eventMonth)
cmd.Parameters.Add("@eventDay", eventDay)
cmd.Parameters.Add("@eventYear", eventYear)
cmd.Parameters.Add("@eventTitle", eventTitle)
cmd.Parameters.Add("@eventTime", eventTime)
cmd.Parameters.Add("@eventAddedBy", eventAddedBy)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
And instead of completing the insert statement, I'm getting the following error, which I don't know how to fix:
Object variable or With block variable not set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object variable or With block variable not set.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack) +929
calendar.WebForm1.addButton_Click(Object sender, EventArgs e) in C:\Documents and Settings\Shaun Worcester\VSWebCache\131.238.126.132\worceste\calendar\WebForm1.aspx.vb:56
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
Any ideas?your code is mostly correct.
you add this lines befor exexcute
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.ExecuteReader
if you already do this then check any object are not created with new keywordYou have the INSERT statement is incorrect, try this instead:
INSERT INTO events (event_month, event_day, event_year, event_title, event_time, even_added_by) VALUES (@eventMonth, @eventDay,@eventYear,@eventTitle,@eventTime,@eventAddedBy)also you had a typo
even_added_by
should be
event_added_by