我正在尝试将代码添加到c#,用户将点击新频道并填充数据,然后按“保存”。挑战是我有多对一的关系,所以我把这些值放在组合框中,所以我将不得不在每个组合框中添加所选项目的ID,所以我写了下面的代码但是我收到了这个错误:
System.NullReferenceException:'对象引用未设置为 对象的实例。'
System.Windows.Forms.ListControl.SelectedValue.get返回null。
虽然我已经以其他形式使用了Tryparse并且它可以正常工作。
此外,我想让用户更新现有记录,同时使用一个保存按钮保存更新或新记录,以便我该怎么做?
谢谢。
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();