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();