Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 09:22

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] error System.InvalidCastException

Discussion on [C#] error System.InvalidCastException within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
Dr.Abdelfattah's Avatar
 
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
Smile [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
Dr.Abdelfattah is offline  
Old 11/28/2011, 15:23   #2
Administrator
 
Muddy Waters's Avatar
 
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.
Muddy Waters is offline  
Thanks
2 Users
Old 11/28/2011, 17:48   #3
 
Dr.Abdelfattah's Avatar
 
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 ,
Dr.Abdelfattah is offline  
Old 11/29/2011, 20:01   #4
Administrator
 
Muddy Waters's Avatar
 
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.
Muddy Waters is offline  
Thanks
1 User
Old 11/29/2011, 23:56   #5
 
Dr.Abdelfattah's Avatar
 
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
Quote:
Originally Posted by Muddy_Waters View Post
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)
Dr.Abdelfattah is offline  
Old 11/30/2011, 13:53   #6
 
DiDaDrüben's Avatar
 
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(); 
DiDaDrüben is offline  
Thanks
1 User
Old 12/02/2011, 14:06   #7
 
Dr.Abdelfattah's Avatar
 
elite*gold: 7
Join Date: May 2010
Posts: 2,115
Received Thanks: 2,374
oh thank you , that's work
Dr.Abdelfattah is offline  
Old 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;
mohammedalhlo is offline  
Reply


Similar Threads 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.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.