我在Windows窗体应用程序中使用WCF服务。我的WCF服务有两个名为FulltimeEmployee
和ParttimeEmployee
的类。我想通过使用基于员工类型的Windows Fomr应用程序的Wcf服务将用户注册到Sql数据库。用户类型将来自emu类型,但我无法编译它并显示以下错误...
这两行上的操作异常无效,它还说ListControl对属性成员的get或set值。
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedValue =="-1")//Error in this line
{
txtHourlyPay.Visible = false;
txtHoursWorked.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
else if (comboBox2.SelectedValue =="1")//Error on this line
{
txtHourlyPay.Visible = true;
txtHoursWorked.Visible = true;
label6.Visible = true;
label7.Visible = true;
}
}
这是FullTime和PartTime Employee Class的代码..
[DataContract]
public class FullTimeEmployee : Employee
{
public int AnnualSalary { get; set; }
}
[DataContract]
public class PartTimeEmployee : Employee
{
public int HourlyPay { get; set; }
public int HoursWorked { get; set; }
}
这是代码表类..
[KnownType(typeof(FullTimeEmployee))]
[KnownType(typeof(PartTimeEmployee))]
[DataContract(Namespace = "http://pragimtech.com/Employee")]
public class Employee
{
private int _id;
private string _name;
private string _gender;
private DateTime _dateOfBirth;
[DataMember(Order = 1)]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataMember(Order = 2)]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember(Order = 3)]
public string Gender
{
get { return _gender; }
set { _gender = value; }
}
[DataMember(Order = 4)]
public DateTime DateOfBirth
{
get { return _dateOfBirth; }
set { _dateOfBirth = value; }
}
[DataMember(Order = 5)]
public EmployeeType Type { get; set; }
}
public enum EmployeeType
{
FullTimeEmployee = 1,
PartTimeEmployee = 2
}
}
以下是根据类型注册员工的WCF服务代码:
public void SaveEmployee(Employee employee)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spSaveEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterId = new SqlParameter
{
ParameterName = "@Id",
Value = employee.Id
};
cmd.Parameters.Add(parameterId);
SqlParameter parameterName = new SqlParameter
{
ParameterName = "@Name",
Value = employee.Name
};
cmd.Parameters.Add(parameterName);
SqlParameter parameterGender = new SqlParameter
{
ParameterName = "@Gender",
Value = employee.Gender
};
cmd.Parameters.Add(parameterGender);
SqlParameter parameterDateOfBirth = new SqlParameter
{
ParameterName = "@DateOfBirth",
Value = employee.DateOfBirth
};
cmd.Parameters.Add(parameterDateOfBirth);
SqlParameter parameterEmployeeType = new SqlParameter
{
ParameterName = "@EmployeeType",
Value = employee.Type
};
cmd.Parameters.Add(parameterEmployeeType);
if (employee.GetType() == typeof(FullTimeEmployee))
{
SqlParameter parameterAnnualSalary = new SqlParameter
{
ParameterName = "@AnnualSalary",
Value = ((FullTimeEmployee)employee).AnnualSalary
};
cmd.Parameters.Add(parameterAnnualSalary);
}
else
{
SqlParameter parameterHourlyPay = new SqlParameter
{
ParameterName = "@HourlyPay",
Value = ((PartTimeEmployee)employee).HourlyPay,
};
cmd.Parameters.Add(parameterHourlyPay);
SqlParameter parameterHoursWorked = new SqlParameter
{
ParameterName = "@HoursWorked",
Value = ((PartTimeEmployee)employee).HoursWorked
};
cmd.Parameters.Add(parameterHoursWorked);
}
con.Open();
cmd.ExecuteNonQuery();
}
}
以下是Windows窗体应用程序代码:
private void button1_Click(object sender, EventArgs e)
{
MyService.HalifaxServiceClient myservice = new MyService.HalifaxServiceClient("NetTcpBinding_IHalifaxService");
MyService.Employee employee = null;
if (((MyService.EmployeeType)Convert.ToInt32(comboBox2.SelectedValue)) == MyService.EmployeeType.FullTimeEmployee)
{
employee = new MyService.FullTimeEmployee
{
Id = Convert.ToInt32(textBox1.Text),
Name = txtName.Text,
Gender = comboBox1.Text,
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
Type = MyService.EmployeeType.FullTimeEmployee(comboBox2.SelectedValue),//Error
AnnualSalary = Convert.ToInt32(txtAnnualSalary.Text),
};
myservice.SaveEmployee(employee);
label8.Text = "Employee saved";
}
else if (((MyService.EmployeeType)Convert.ToInt32(comboBox2.SelectedValue)) == MyService.EmployeeType.PartTimeEmployee)
{
employee = new MyService.PartTimeEmployee
{
Id = Convert.ToInt32(textBox1.Text),
Name = txtName.Text,
Gender = comboBox1.Text,
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
Type = MyService.EmployeeType.PartTimeEmployee(comboBox2.SelectedValue),//Error
HourlyPay = Convert.ToInt32(txtHourlyPay.Text),
HoursWorked = Convert.ToInt32(txtHoursWorked.Text),
};
myservice.SaveEmployee(employee);
label8.Text = "Employee saved";
}
else
{
label8.Text = "Please select Employee Type";
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedValue =="-1")
{
txtHourlyPay.Visible = false;
txtHoursWorked.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
else if (comboBox2.SelectedValue =="1")
{
txtHourlyPay.Visible = true;
txtHoursWorked.Visible = true;
label6.Visible = true;
label7.Visible = true;
}
}
当我点击提交按钮时,它会显示错误消息
并且不会将新员工插入SQL Server数据库..
以下是输出的截图:
请帮我纠正这个错误...
答案 0 :(得分:1)
我认为你需要SelectedItem,而不是你的组合框上的SelectedValue。以此代码为例,我认为这可以模拟您的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox2.Items.Add(Employee.FullTimeEmployee);
comboBox2.Items.Add(Employee.PartTimeEmployee);
comboBox2.SelectedIndexChanged += ComboBox1OnSelectedIndexChanged;
label1.Text = "Full text: ";
label2.Text = "Integer value: ";
}
private void ComboBox1OnSelectedIndexChanged(object sender, EventArgs eventArgs)
{
label1.Text = "Full text: " + ((ComboBox) sender).SelectedItem;
label2.Text = "Integer value: " + (int)(Employee)(((ComboBox)sender).SelectedItem);
}
}
public enum Employee
{
FullTimeEmployee = 1,
PartTimeEmployee = 2
}
从那里你应该得到这些结果:
您可以在代码中看到比较文本,您可以抓取所选项目,但要获得将其转换为int所需的整数值。希望这有帮助,快乐编码!