Click here to Skip to main content
14,879,456 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Guys, this does a search based on a string being input, I am rewriting the code so that i can have it do the search exactly the same way but based on an integer, i tried to change instances of string to "int" or "int64" but i know that it wouldnt work, was worth a try, just curious on how i can get it working to read "int claimID" instead of "string ClaimID"
any help would be greatly appreciated!
C#
public string[] searchClaim(string ClaimID)
        {
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            try
            {
                //SQL command to slect the claim data and add to an array to pass back
                string sql = "SELECT * FROM Claim WHERE ClaimID =" + ClaimID + "";
                OleDbCommand command = new OleDbCommand(sql, con);
                OleDbDataReader reader = command.ExecuteReader();
                reader.Read();
                string[] dbResult = new String[5]; //error here when i change it to int64 instead of string
                dbResult[0] = "Success";
                dbResult[1] = reader[0].ToString(); //also errors here saying int cannot be converted to long
                dbResult[2] = reader[1].ToString(); 
                dbResult[3] = reader[2].ToString(); 
                dbResult[4] = reader[3].ToString(); 
                con.Close();
                return dbResult; //error here as well
            }
            catch
            {
                throw new Exception();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }
        }
Posted
Updated 25-Apr-12 9:28am
v2
Comments
[no name] 25-Apr-12 14:08pm
   
Your question is not clear. The indicated line could not possibly give you the error described.
Member 7791974 25-Apr-12 15:27pm
   
when i change to "int" it does, sorry i'll add this to the code
[no name] 25-Apr-12 15:38pm
   
If you are saying the you are trying Int64[] dbResult = new string[5]; and getting that error, why would that surprise you?
A. Orozco 25-Apr-12 15:52pm
   
Are you expecting the results from "SELECT * FROM Claim WHERE ClaimID =" + ClaimID + "" to be all Int64 (i.e. all the columns are Int64) so you can load them into and array of Int64? If not, you will have to change your approach.

Also, doing this: dbResult[0] = "Success"; will fail if dbResult is an array of Int64 because, wait for it..... "Success" is not an Int64!

You can do it (read data) in many different ways, for example,
1) using Read() method for OleDbDataReader: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader%28v=vs.80%29.aspx#Y0[^]
2) or using DataTable.Load(OleDbDataReader) method
http://msdn.microsoft.com/en-us/library/system.data.datatable.load.aspx[^]
3) or DataSet.Load() method: http://msdn.microsoft.com/en-us/library/yab24tfx.aspx[^]

I hope it will help...

[EDIT]SECOND THOUGHT[/EDIT]
If you really need to use array of string, please, read more about type conversion, especially about explicit conversion.
http://www.dotnetspider.com/resources/27313-implicit-explicit-conversion-c.aspx[^]
http://msdn.microsoft.com/en-us/library/ms173105.aspx[^]
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/808ad927-385f-49c9-aace-256eefe659d7[^]
http://msdn.microsoft.com/en-us/library/ms173105%28v=vs.80%29.aspx[^]
   
v3
To get this to work like you think it would work is:
C#
public int[] searchClaim(string ClaimID)
        {
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            try
            {
                //SQL command to slect the claim data and add to an array to pass back
                string sql = "SELECT * FROM Claim WHERE ClaimID =" + ClaimID + "";
                OleDbCommand command = new OleDbCommand(sql, con);
                OleDbDataReader reader = command.ExecuteReader();
                reader.Read();
                int[] dbResult = new int[5]; 
                dbResult[0] = 1;
                dbResult[1] = reader[0];
                dbResult[2] = reader[1]; 
                dbResult[3] = reader[2]; 
                dbResult[4] = reader[3]; 
                con.Close();
                return dbResult;             }
            catch
            {
                throw new Exception();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }
        }


It would appear that you are getting int datatypes from your database so you need an int array not an Int64. int != Int64 && int != string

As losmac rightfully pointed out, this would only work this way IF your database schema is setup with int datatypes. If your datatypes are mixed, this will not work.
   
v3
Comments
Maciej Los 25-Apr-12 16:47pm
   
Are you sure that the rest of fields are integers?
[no name] 25-Apr-12 16:51pm
   
Well no I am not sure, he has not provided that information. Hence the disclamer, "it appears"....
[no name] 25-Apr-12 17:30pm
   
Updated. Thanks for pointing that out.
Maciej Los 26-Apr-12 13:48pm
   
;)
Member 7791974 26-Apr-12 3:14am
   
thank you so much for responding, it is a claim table so there will be datatypes that include int and string and so forth, what is the best way to go about it?
now my error is appearing at:
dbResult[1] = reader[0];
dbResult[2] = reader[1];
dbResult[3] = reader[2];
dbResult[4] = reader[3];
the error says "Error 2 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)"
[no name] 26-Apr-12 6:44am
   
Without knowing more about your schema, it's not possible to say. Why do you want to stuff all of this into an int array anyway? I mean if the data is not an int why force it to be? What was wrong the string array?

Edit... On second thought, what I would probably do is create a class to encapsulate the data and pass that around.

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