Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

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

any help would be much apreciated,thanks in advance
Posted
Updated 4-Aug-15 21:29pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Aug-15 3:34am    
In what line?
—SA
jamesmc1535 5-Aug-15 3:37am    
i dont know visual studios starts perfectly , its when i run the program and open that form (add user) that i get that error

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

 
Share this answer
 

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: If you are going to use using blocks - and it's a damn good idea - then put one round the SqlConnection object as well!

 
Share this answer
 
Comments
jamesmc1535 5-Aug-15 3:47am    
thanks for the advice i will check quickly , when i run it and i get te eror i have the options,, details and continue,and quit but when i quit it doesnt show the line thats faulty, if i say continue the combobox is still emty XD (oviously) but the details shows this


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
at login_form.add_user.<>c.<add_user_load>b__2_0(DataRow dataRow) in C:\Users\james\New folder\POS\pos\login form\add user.cs:line 53
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at login_form.add_user.add_user_Load(Object sender, EventArgs e) in C:\Users\james\New folder\POS\pos\login form\add user.cs:line 51
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
login form
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/james/New%20folder/POS/pos/login%20form/bin/Debug/login%20form.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.6.81.0 built by: NETFXREL2
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/S
OriginalGriff 5-Aug-15 4:02am    
And when you get that box, it is showing the line in your source.
So. Put a breakpoint on the line
var empList = Ds.Tables[0].AsEnumerable().Select(dataRow => dataRow.Field<int>("username")).ToList();
and look at the DataTable content before it is executed.
Or even - look at the user1 table in SSMS! :laugh:
jamesmc1535 5-Aug-15 4:18am    
lol XD this sqls doesnt like me, struggle alot with it -_- my mind just doesnt want to learn thanks il go do a quick check :)
jamesmc1535 5-Aug-15 4:48am    
i entered it as u said but my datarow says does not exist , shud i create it or am i missing a refrence?
OriginalGriff 5-Aug-15 5:41am    
Share the exact code you used, and the exact error message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900