I am trying to update information using combobox to the database and the error it gives is "Object reference not set to an instance of an object"
-
Below is my stored procedure ALTER PROCEDURE [dbo].[sp_Workflow] @TicketID int output, @StaffID int output, @Department nvarchar (50), @FirstName nvarchar (50), @TicketStatusID int output, @TicketStatus nvarchar (50), @TicketPriorityID int output, @TicketPriority nvarchar (50) AS BEGIN UPDATE dbo.Staff SET FirstName = @FirstName, Department = @Department WHERE StaffID= @StaffID UPDATE dbo.TicketStatus SET TicketStatus = @TicketStatus WHERE TicketStatusID =@TicketStatusID UPDATE dbo.TicketPriority SET TicketPriority = @TicketPriority WHERE TicketPriorityID =@TicketPriorityID END T :) his is my code private void Updatebutton_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd = new SqlCommand("sp_Workflow", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@TicketID", System.Data.SqlDbType.Int).Value = TicketIDcomboBox.ToString(); cmd.Parameters.Add("@Department", System.Data.SqlDbType.NVarChar, 50).Value = MoveToDepartmentcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar, 50).Value = StaffcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketStatus", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketStatuscomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketPriorty", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketPrioritycomboBox.SelectedValue.ToString(); MessageBox.Show("Are you sure you want to update?"); cmd.ExecuteNonQuery(); MessageBox.Show("Ticket Updated"); con.Close(); } }
-
Below is my stored procedure ALTER PROCEDURE [dbo].[sp_Workflow] @TicketID int output, @StaffID int output, @Department nvarchar (50), @FirstName nvarchar (50), @TicketStatusID int output, @TicketStatus nvarchar (50), @TicketPriorityID int output, @TicketPriority nvarchar (50) AS BEGIN UPDATE dbo.Staff SET FirstName = @FirstName, Department = @Department WHERE StaffID= @StaffID UPDATE dbo.TicketStatus SET TicketStatus = @TicketStatus WHERE TicketStatusID =@TicketStatusID UPDATE dbo.TicketPriority SET TicketPriority = @TicketPriority WHERE TicketPriorityID =@TicketPriorityID END T :) his is my code private void Updatebutton_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd = new SqlCommand("sp_Workflow", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@TicketID", System.Data.SqlDbType.Int).Value = TicketIDcomboBox.ToString(); cmd.Parameters.Add("@Department", System.Data.SqlDbType.NVarChar, 50).Value = MoveToDepartmentcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar, 50).Value = StaffcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketStatus", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketStatuscomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketPriorty", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketPrioritycomboBox.SelectedValue.ToString(); MessageBox.Show("Are you sure you want to update?"); cmd.ExecuteNonQuery(); MessageBox.Show("Ticket Updated"); con.Close(); } }
The trick to solving an issue like this is to put a breakpoint in the start of your code and step through it, inspecting the values of various variables to see what they contain. For instance, looking at your code, it strikes me that the problem might possibly be with the line
con.Open();
. What if con is null? Where are you allocating it? BTW - you are showing a messagebox and then doing nothing with the result to prevent the query being executed. If I were you, I would move this to the top of the method and only proceed if the user clicks Yes. -
Below is my stored procedure ALTER PROCEDURE [dbo].[sp_Workflow] @TicketID int output, @StaffID int output, @Department nvarchar (50), @FirstName nvarchar (50), @TicketStatusID int output, @TicketStatus nvarchar (50), @TicketPriorityID int output, @TicketPriority nvarchar (50) AS BEGIN UPDATE dbo.Staff SET FirstName = @FirstName, Department = @Department WHERE StaffID= @StaffID UPDATE dbo.TicketStatus SET TicketStatus = @TicketStatus WHERE TicketStatusID =@TicketStatusID UPDATE dbo.TicketPriority SET TicketPriority = @TicketPriority WHERE TicketPriorityID =@TicketPriorityID END T :) his is my code private void Updatebutton_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd = new SqlCommand("sp_Workflow", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@TicketID", System.Data.SqlDbType.Int).Value = TicketIDcomboBox.ToString(); cmd.Parameters.Add("@Department", System.Data.SqlDbType.NVarChar, 50).Value = MoveToDepartmentcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar, 50).Value = StaffcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketStatus", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketStatuscomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketPriorty", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketPrioritycomboBox.SelectedValue.ToString(); MessageBox.Show("Are you sure you want to update?"); cmd.ExecuteNonQuery(); MessageBox.Show("Ticket Updated"); con.Close(); } }
The first step is to figure out where the null reference error is happening. Why not put a break-point at the entry to the Updatebutton_Click EventHandler code, and single-step through the code until you find the error. If you call the 'ToString method on a ComboBox's SelectedValue when there is no value selected, you will get a null reference error: that's one of the possible sources of error to evaluate.
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts. Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
-
Below is my stored procedure ALTER PROCEDURE [dbo].[sp_Workflow] @TicketID int output, @StaffID int output, @Department nvarchar (50), @FirstName nvarchar (50), @TicketStatusID int output, @TicketStatus nvarchar (50), @TicketPriorityID int output, @TicketPriority nvarchar (50) AS BEGIN UPDATE dbo.Staff SET FirstName = @FirstName, Department = @Department WHERE StaffID= @StaffID UPDATE dbo.TicketStatus SET TicketStatus = @TicketStatus WHERE TicketStatusID =@TicketStatusID UPDATE dbo.TicketPriority SET TicketPriority = @TicketPriority WHERE TicketPriorityID =@TicketPriorityID END T :) his is my code private void Updatebutton_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd = new SqlCommand("sp_Workflow", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@TicketID", System.Data.SqlDbType.Int).Value = TicketIDcomboBox.ToString(); cmd.Parameters.Add("@Department", System.Data.SqlDbType.NVarChar, 50).Value = MoveToDepartmentcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar, 50).Value = StaffcomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketStatus", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketStatuscomboBox.SelectedValue.ToString(); cmd.Parameters.Add("@TicketPriorty", System.Data.SqlDbType.NVarChar, 50).Value = ChangeTicketPrioritycomboBox.SelectedValue.ToString(); MessageBox.Show("Are you sure you want to update?"); cmd.ExecuteNonQuery(); MessageBox.Show("Ticket Updated"); con.Close(); } }
Try debugging your code when this type of error occurs. This generally happens when you access a property or method of an object that itself is null.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial