嗨,我得到这个错误演员无效 [英] Hi I Get This Eror Cast Not Valid

查看:62
本文介绍了嗨,我得到这个错误演员无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

morining所有我试图将表中的数据显示到一个组合框(组合框中的用户名)但我排序了大多数错误。

我的错误System.InvalidCastException:指定演员无效。

我也只在调试程序并打开该表格时收到此消息





我的代码>

private void add_user_Load(object sender,EventArgs e)

{

DataSet Ds = new DataSet();



string strConnectionString =Data Source = JAMES-PC\\SQLEXPRESS; Initial Catalog = login1; Integrated Security = True;



SqlConnection objconnection = new SqlConnection(strConnectionString);

using(SqlCommand cmd = new SqlCommand(SELECT [username] FROM [user1],

objconnection))

{

使用(SqlDataAdapter adapter = new SqlDataAdapter(cmd))

{

adapter.Fill(Ds);

}

}



var empList = Ds.Tables [0] .AsEnumerable()。选择(dataRow =>



dataRow.Field< int>(username )。ToList();

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

comboBox1.DataSource = empList;



comboBox1.SelectedIndex = 0;



morining all im trying to display data from a table into a combo box(usernames in a combo box) but i sorted most errors that i got .
"my error" System.InvalidCastException: Specified cast is not valid.
also i only get this message when i debug the program and open that form


my code>
private void add_user_Load(object sender, EventArgs e)
{
DataSet Ds = new DataSet();

string strConnectionString = "Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True";

SqlConnection objconnection = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("SELECT [username] FROM [user1]",
objconnection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(Ds);
}
}

var empList = Ds.Tables[0].AsEnumerable().Select(dataRow =>

dataRow.Field<int>("username")).ToList();
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DataSource = empList;

comboBox1.SelectedIndex = 0;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString();
        }



任何帮助都会有所帮助,提前谢谢


any help would be much apreciated,thanks in advance

推荐答案

您好,

此类错误适用于无效转换或显式转换。请检查username是否为Integer或其他数据类型。

Hello ,
This type of error comes for Invalid casting or explicit conversion.Check whether "username" is Integer or other datatype.


在调试器中运行应用程序:当异常发生时,它将停止,VS将向您显示它拾取问题的行。

很可能与您的Linq选择匿名方法,并且您的用户名不是全部整数。因此,当断点点击时,查看DataTable中的数据并检查。

Run the application in the debugger: when the exception occurs, it will stop and VS will show you the line it picked up the problem with.
The chances are that it's to do with your Linq Select anonymous method, and that your usernames are not all integers. So when the breakpoint hits, look at the data in your DataTable and check.

我们无法为您执行此操作 - 我们无法运行您的代码,我们不能访问你的数据库!

We can't do that for you - we can't run your code, and we don;t have access to your database!

BTW:如果你打算使用使用块 - 而且它是一个该死的好主意 - 然后把一个回合放在SqlConnection对象上!

BTW: If you are going to use using blocks - and it's a damn good idea - then put one round the SqlConnection object as well!


这篇关于嗨,我得到这个错误演员无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆