2

I am trying to add the code to c# where the user will click on new channel and fill the data and then press save. The challenge is that i have many to one relations so i placed these values in comboboxes so i will have to add the id of the selected item in each combobox so i wrote the following code but i am receiving this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Windows.Forms.ListControl.SelectedValue.get returned null.

Although i have used the Tryparse in other forms and it works correctly.

In addition i want to let the user to update an existing record while using one save button to either save the updates or the new record so how can i do it?

Thank you.

           string sql = "insert into ([Name],[Channel Category ID],[Channel Type],[Channel Status],[Governator ID],[District ID],[City ID],[Street],[Account],[Surface],[Short term Price per night],[Long term price per month],[Selling Price])values(,@[Name],@[Channel Category ID],@[Channel Type],@[Channel Status],@[Governator ID],@[District ID],@[City ID],@[Street],@[Account],@[Surface],@[Short term Price per night],@[Long term price per month],@[Selling Price])";

        string ConnectionString;
        ConnectionString = "Data Source = ANTHONYZ\\SQLEXPRESS01; Initial Catalog = Tenant Management; Integrated Security = True";
        SqlConnection conn = new SqlConnection(ConnectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;
                int val;
                Int32.TryParse(channelcategory.SelectedValue.ToString(), out val);
                int val1;
                Int32.TryParse(channeltype.SelectedValue.ToString(), out val1);
                int val2;
                Int32.TryParse(channelstatus.SelectedValue.ToString(), out val2);
                int val3;
                Int32.TryParse(governator.SelectedValue.ToString(), out val3);
                int val4;
                Int32.TryParse(district.SelectedValue.ToString(), out val4);
                int val5;
                Int32.TryParse(city.SelectedValue.ToString(), out val5);
                cmd.Parameters.AddWithValue("@[Name]", channelname.Text);
                cmd.Parameters.AddWithValue("@[Channel Category ID]", val);
                cmd.Parameters.AddWithValue("@[Channel Type]", val1);
                cmd.Parameters.AddWithValue("@[Channel Status]", val2);
                cmd.Parameters.AddWithValue("@[Governator ID]", val3);
                cmd.Parameters.AddWithValue("@[District ID]", val4);
                cmd.Parameters.AddWithValue("@[City ID]", val5);
                cmd.Parameters.AddWithValue("@[Street]", street.Text.ToString());
                cmd.Parameters.AddWithValue("@[Account]", Convert.ToInt32(account));
                cmd.Parameters.AddWithValue("@[Surface]", Convert.ToInt32(surface.Text));
                cmd.Parameters.AddWithValue("@[Short term Price per night]", shrtrntprice.Text);
                cmd.Parameters.AddWithValue("@[Selling Price]", sellprice.Text);
                cmd.Parameters.AddWithValue("@[Long term price per month]", lngrent.Text);
        cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        conn.Close();
  • Try `.SelectedValue?.ToString()` (calling ToString on a null object won't work, so you can use this to exit early) – ProgrammingLlama Jun 06 '18 at 16:27
  • Debug to figure out which line is throwing the NullReferenceExeption then examine variable values on that line to see what exactly is null. – Eric J. Jun 06 '18 at 16:59
  • @EricJ. The NullReferenceException is showing on the first tryparse even if i try to remove it the second one will show the same –  Jun 07 '18 at 07:00
  • @john I tried the same code on other forms and it is working correctly if i add the ? And it works correctly it sould be just to escape the null value but i need to save this value in the database so would the ? Fix it? –  Jun 07 '18 at 07:02
  • 1
    @Anthoz I recommend proactively dealing with nulls, tbh. – ProgrammingLlama Jun 07 '18 at 07:28
  • Thank you for your help guys but my problem has not been fixed. when i added the ? the val = val2 = 0 even when they should not be 0. The purpose is to get the id of the item selected in the combobox since i am using in this form two tables with a one to many relation so i have the id of the item selected in the combobox in the table where i am searching. –  Jun 08 '18 at 11:46

0 Answers0