|
You last visited: Today at 09:22
Advertisement
[C#] error System.InvalidCastException
Discussion on [C#] error System.InvalidCastException within the .NET Languages forum part of the Coders Den category.
11/28/2011, 13:06
|
#1
|
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
|
[C#] error System.InvalidCastException
Hello!
I was Coding a project wich connect and read something from sql database,
PHP Code:
MsSQL my = new MsSQL("select * from TableExample"); using (System.Data.SqlClient.SqlDataReader reader = my.Read()) { while (reader.Read()) { accInformation acc = new accInformation(); Console.WriteLine(reader.GetString("User")); acc.User = reader.GetString("User"); acc.password = reader.GetString("password"); acc.Name = reader.GetString("Name"); acc.Email = reader.GetString("Email"); acc.sex = reader.GetString("sex"); acc.address = reader.GetString("address"); acc.postcode = reader.GetString("postcode"); acc.phone = reader.GetString("phone"); acc.mobile = reader.GetString("mobile"); acc.reg_ip = reader.GetString("reg_ip"); Global.AccountList.Add(acc); max++; } } my.Close();
but
reader.GetString("exmple")
gets an error System.InvalidCastException ,
The Best overloaded method match for system.data.comman.dbdatareader.getstring(int) has some invalid arguments
-------------------------
any idea?
ty
|
|
|
11/28/2011, 15:23
|
#2
|
Administrator
elite*gold: 41624
Join Date: Jan 2010
Posts: 22,728
Received Thanks: 12,654
|
You should always check the  before even asking. The GetString() method obviously expects an integer that specifies the column index.
Instead you are passing strings that aren't even representing numbers, but letters. Which is why trying to cast these strings to integer implicitly will raise an exception.
The solution is rather simple: you should rethink what it is that you are trying to accomplish and then use proper methods.
|
|
|
11/28/2011, 17:48
|
#3
|
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
|
Put can u write a small explain ^^ about how to change GetString (to gives it int i)
as if u look here
acc.Email = reader.GetString("Email");
and
accInformation acc = new accInformation();
accInformation is class to another item
sorry i'm not as good in english enough to explain what i mean ,
|
|
|
11/29/2011, 20:01
|
#4
|
Administrator
elite*gold: 41624
Join Date: Jan 2010
Posts: 22,728
Received Thanks: 12,654
|
Unfortunately, I haven't worked with SQL and C# myself, so I am probably unable to provide any profound intel on that. Also, giving you any advice on what methods to use would require to actually know how your data is structured.
So I'd suggest you explain a little more thoroughly how your SQL data is structured by preferably giving a proper example.
|
|
|
11/29/2011, 23:56
|
#5
|
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
|
Quote:
Originally Posted by Muddy_Waters
Unfortunately, I haven't worked with SQL and C# myself, so I am probably unable to provide any profound intel on that. Also, giving you any advice on what methods to use would require to actually know how your data is structured.
So I'd suggest you explain a little more thoroughly how your SQL data is structured by preferably giving a proper example. 
|
First thanks for try to help 
well let me talk first about my project , it's copy the new data from table(database) 2 to table 1
so we can say we got mssql1 and mssql2 , mssql = the first users table
mssql 2 = the secound users table (the tables got the same name and same content [content like struserid, password, Email, etc..])
mssql1 is the original users table so mssql1 refers to a mssql1.cs (i mean to another content) that contain the read module
and mssql2.cs that contain more then modul reader like write and clean and some more .
If u need more info just tell me
ah(about MsSQL it's refers to mssql1 as i name them mssql and mssql1)
|
|
|
11/30/2011, 13:53
|
#6
|
elite*gold: 1
Join Date: Aug 2011
Posts: 172
Received Thanks: 14
|
Here is the suggestion from Muddy_Waters in code. Just name the columns in the SQL statement and use to column indices with GetString(). Also check if every column is of type string.
PHP Code:
MsSQL my = new MsSQL("select User,password, Name,Email,sex,address,postcode,phone,mobile,reg_ip from TableExample");
using (System.Data.SqlClient.SqlDataReader reader = my.Read())
{
while (reader.Read())
{
accInformation acc = new accInformation();
if (!reader.IsDBNull(0)) {
acc.User = reader.GetString(0);
}
if (!reader.IsDBNull(1)) {
acc.password = reader.GetString(1);
}
//TODO continue :)
Global.AccountList.Add(acc);
max++;
}
}
my.Close();
|
|
|
12/02/2011, 14:06
|
#7
|
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
|
oh thank you , that's work
|
|
|
08/24/2013, 23:09
|
#8
|
elite*gold: 0
Join Date: Aug 2013
Posts: 3
Received Thanks: 0
|
please help here the same problem but access database not sql
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(conStr);
string str = ("select * from اساسي where الاسم الكامل='"+comboBox1.Text+"'");
OleDbCommand cmd = new OleDbCommand(str, conn);
OleDbDataReader myreader;
try
{
conn.Open();
myreader = cmd.ExecuteReader();
while (myreader.Read())
{
string sname = myreader.GetString("اسم الام");
textBox1.Text = sname;
|
|
|
 |
Similar Threads
|
[EP2] Shop System Error!
11/17/2011 - Last Chaos - 3 Replies
Hey,
habt ihr auch immer System Error, wenn ihr den Shop öffnet? Da kommt nur so ein Fenster System Error und mir wird angezeigt, dass ich 0 Cash habe! Bei meinen ganzen Kumpels ist das auch so! Aber hab zum Test mal 'nen neuen Acc erstellt und bei dem geht es! Wisst ihr wie man das fixen kann?
MfG
|
System Error in [EG]??
03/25/2011 - 4Story - 17 Replies
Hey,
jetz um 2:10 wurde ich gekickt und hab versucht mich wieder einzuloggen dann stad da : System Error. Please try it again later. Was heißt das??
Thx an alle die es mir sagen können :D
@Krakatoas
Ich spiele 4story^^ aber egal^^ Ok Seite 5----> Tut mir leid aber habe ich nciht sehen können^^ naja gut ich kümmer mich drum
|
[Error] V 16 Map System
03/20/2011 - Flyff Private Server - 2 Replies
Hey, habe keine ahnung wieso, meine neuz das v16 Map System nicht annehmen will :s
Nunja hier ist mein source von Zeile 167- 172 aus..
CWndButton* pWndMonsterInfoCheckBox = ( CWndButton* )GetDlgItem( WIDC_CHECK_MONSTER_INFORMATION );
CWndButton* pWndMonsterInfoCheckBox = ( CWndButton* )GetDlgItem( WIDC_BUTTON_MONSTER_INFO );
{
pWndMonsterInfoCheckBox->SetVisible( TRUE );
}
}
|
4Story EG ! System Error
09/29/2010 - 4Story - 2 Replies
Mein Problem ist ich logge mich mit meine acc ein und dann der 1te cha geht aber bei meinen 2-3-4 wenn ich damit rein will kriege ich kick und steht system error mein lvl 72er ist auch dabei gestern habe ich noch gespielt -.-
Weis jemand eine Lösung?
|
system.mrs error
03/23/2008 - GunZ - 0 Replies
Hey guys how is it going i was doing a tut on system.mrs i did all the editing and started the game and i wonts to update the file/delete my work. how can you do this how do you get past the updating part thx. :)
|
All times are GMT +1. The time now is 09:23.
|
|