|
-
Apr 23rd, 2025, 08:10 AM
#1
Thread Starter
Hyperactive Member
Object reference no set to an instance or object?
Hi guys Greeting
I am using SQL and VB.NET 2017. I have 2 buttons on a form1, (Add Save), the procedure is almost similar. Add a new record and save an edited record. I have a textbox "Email" in both buttons, which is the problem. I can not change the name because there is only one field in my table with the name Email. I show you the code of both buttons, Add and Save. I have no more ideas on how to fix it. I have tried many times in different ways. I had to declare global the textbox, declare friend, publics, and nothing fixed it, that is why I need your help, because you're a professional. This problem arises after changing the name of a field in my database table. The field was before "Interested" and is now "Email". Here is my code, very similar to the add button. Thank you, Blessing guys!
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Dim query As String = "INSERT INTO Citas (ID, Customer, Email, Contact, Telephone, Company, & _
Address, AppoDate, AppoTime, About, Solution, NextDate, NextTime) VALUES (@ID, @Customer, @Email , & _
@Contact, @Telephone, @Company, @Address, @AppoDate, @AppoTime, @About, @Solution, @NextDate, @NextTime)"
Dim command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@ID", txtID.Text)
command.Parameters.AddWithValue("@Customer", txtCustomer.Text)
command.Parameters.AddWithValue("@Email", txtEmail.Text)
command.Parameters.AddWithValue("@Contact", txtContact.Text)
command.Parameters.AddWithValue("@Telephone", txtTelephone.Text)
command.Parameters.AddWithValue("@Company", txtCompany.Text)
command.Parameters.AddWithValue("@Address", txtAddress.Text)
command.Parameters.AddWithValue("@AppoDate", txtAppoDate.Text)
command.Parameters.AddWithValue("@AppoTime", txtAppoTime.Text)
command.Parameters.AddWithValue("@ABout", txtAbout.Text)
command.Parameters.AddWithValue("@Solution", txtSolution.Text)
command.Parameters.AddWithValue("@NextDate", txtNextDate.Text)
command.Parameters.AddWithValue("@NextTime", txtNextTime.Text)
connection.Open()
command.ExecuteNonQuery()
connection.Close()
MessageBox.Show("Record Saved!")
LoadData()
End Sub
-
Apr 23rd, 2025, 08:28 AM
#2
Re: Object reference no set to an instance or object?
Which line has the exception? That error means that an object is Nothing, so take a look at the line where the exception is thrown. Which object is Nothing? Once you know that, then you may also see why it is Nothing.
In the code you showed, there are very few objects that COULD be Nothing, so I would guess that the exception is happening on this line:
connection.Open()
My usual boring signature: Nothing
 
-
Apr 24th, 2025, 07:45 AM
#3
Re: Object reference no set to an instance or object?
As a suggestion, you are displaying "Record Saved!" but I don't see any logic to check if it was or not. Granted once SQL is stable it usually is "stable" but as I general practice I check the returns on database calls. Just a thought...
Please remember next time...elections matter!
-
Apr 24th, 2025, 09:34 AM
#4
Re: Object reference no set to an instance or object?
I responded to your thread on another forum about this problem but you provided information here that you didn't there. You indicated that the exception was thrown on this line:
Code:
command.Parameters.AddWithValue("@Email", txtEmail.Text)
but you didn't tell us this:
This problem arises after changing the name of a field in my database table. The field was before "Interested" and is now "Email".
I don't know what you actually did, because you haven't bothered to explain, but it seems like you just added a TextBox variable with that name but never actually created a TextBox. You should have simply changed the name of the existing control but, unless VS did glitched out at the time, you must not have done that Just open your form in the designer and make sure that you have a TextBox with that name, first deleting any other variable with the same name. If you already have that then, as I said at the other site, you must be setting that txtEmail field to Nothing somewhere, but you never actually bothered to post back to indicate whether that was the case or not.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|