[C#] error System.InvalidCastException

11/28/2011 13:06 Dr.Abdelfattah#1
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 Muddy Waters#2
You should always check the [Only registered and activated users can see links. Click Here To Register...] 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 Dr.Abdelfattah#3
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 Muddy Waters#4
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 Dr.Abdelfattah#5
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)
11/30/2011 13:53 DiDaDrüben#6
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 Dr.Abdelfattah#7
oh thank you , that's work :)
08/24/2013 23:09 mohammedalhlo#8
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;