C# 单击按钮时引发System.FormatException,但程序在单击OK-WPF后打开
我输入姓名、通用密码和密码,当我单击“登录”时,我得到一个系统。格式异常:输入字符串的格式不正确。但是,单击“确定”后,我打算打开的窗口将被打开。我如何摆脱异常。异常表示错误被抛出到这堆代码中:C# 单击按钮时引发System.FormatException,但程序在单击OK-WPF后打开,c#,sql,.net,database,wpf,C#,Sql,.net,Database,Wpf,我输入姓名、通用密码和密码,当我单击“登录”时,我得到一个系统。格式异常:输入字符串的格式不正确。但是,单击“确定”后,我打算打开的窗口将被打开。我如何摆脱异常。异常表示错误被抛出到这堆代码中: `private void ShowEmployeeRank1() { // use a try-catch, in case we run into an // error while digging into the database
`private void ShowEmployeeRank1()
{
// use a try-catch, in case we run into an
// error while digging into the database
try
{
// create a query and select everything from the EmployeeRankq table
string query = "select * from EmployeeRank1";
// the SqlDataAdapter can be imagined like an
// Interface to make tables usable by C# objects
// create a connection to the database and run the query
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, sqlConnection);
// use the sqlDataAdapter
using (sqlDataAdapter)
{
// create a new DataTable that allows us
// to store data from tables within objects
DataTable employeeRank1Table = new DataTable();
// fill the sqlDataAdapter with all the
// information from the query(from the employeeRank1Table)
sqlDataAdapter.Fill(employeeRank1Table);
// set the content of the employeeRank1List
// to be "Name" from the table "EmployeeRank1"
employeeRank1List.DisplayMemberPath = "Name";
// set the value of a specific item
// of listEmployeeRank1 to be "Id" from "EmployeeRank1" table
employeeRank1List.SelectedValuePath = "Id";
// set the content in the employeeRank1List to be the
// content of the EmployeeRank1 table in a DefaultView mode
employeeRank1List.ItemsSource = employeeRank1Table.DefaultView;
}
}
catch (Exception e)
{
// show what is the error
MessageBox.Show(e.ToString());
}
}
更具体地说,这行代码:
employeeRank1List.ItemsSource=employeeRank1Table.DefaultView代码>这修复了异常,但现在我在EmployeeRank1表的列表框中的记录不再可见。因为该建议没有意义。拆下ToString。对于您的问题,请尝试提供一个。您是否尝试将datatable设置为源而不是defaultview?例如:employeeRank1List.ItemsSource=employeeRank1Table代码>;您没有进行任何筛选,为什么要使用defaultview?还有,employeeRank1List是什么类型的对象?@zaggler刚刚尝试过,抛出了一个错误-它们属于不同的类型。事实上,我在进行过滤,我试图显示表中的某一列,而不是全部。EmployeeRank1列表是一个列表框。@Clemens谢谢,我需要更多地关注评论:),我已经准备好周末了,看起来。。。