sticar
asked on
DataReader help get value... ahh what am I doing wrong?
Error is : Object reference not set to an instance of an object.
string [] linkDesc;
string [] linkId;
cmd.CommandText = s;
cmd.Connection = conn;
conn.Open();
dtaReader = cmd.ExecuteReader();
while (dtaReader.Read())
{
bombs on this line: linkDesc[lcnt] = dtaReader["s_desc"].ToStri ng();
linkId[lcnt] = dtaReader["s_search"].ToSt ring();
lcnt++;
}
string [] linkDesc;
string [] linkId;
cmd.CommandText = s;
cmd.Connection = conn;
conn.Open();
dtaReader = cmd.ExecuteReader();
while (dtaReader.Read())
{
bombs on this line: linkDesc[lcnt] = dtaReader["s_desc"].ToStri
linkId[lcnt] = dtaReader["s_search"].ToSt
lcnt++;
}
ASKER
redim? Isn't that an old VB command?
this line string [] linkDesc
should be string [] linkDesc = new string[10];
if you dont know the what size the array should be then consider using an arraylist
should be string [] linkDesc = new string[10];
if you dont know the what size the array should be then consider using an arraylist
ASKER
Unfortunately, I'm trying to fix someone elses code and I don't want to have to rewrite everything. string[] linkDesc and string[] linkId are both passed into this function.
Can I not just get the values from the "Select *..." and store it in the string[] ?
Can I not just get the values from the "Select *..." and store it in the string[] ?
then ensure that the calling function has instantiated the arrays before passing the same to the function
The comments posted here all point to the same problem:
One of your objects has not been instantiated.
The samlpe code does not seem complete, as you say some parameters are passed in? ("string[] linkDesc and string[] linkId are both passed into this function.")
If that is the case, and the arrays are already dimensioned, then one of your other objects needs to be instantiated, eg:
cmd = new SqlCommand ' <- or whatever command object you're using.
or the connection object?
conn = new SqlConnection
Check through yur code to ensure theses objects get instantiated.
Note, the DataReader object does NOT need to be instantiated, as it gets an instance from the cmd.ExecuteReader function.
Hope this helps,
David
One of your objects has not been instantiated.
The samlpe code does not seem complete, as you say some parameters are passed in? ("string[] linkDesc and string[] linkId are both passed into this function.")
If that is the case, and the arrays are already dimensioned, then one of your other objects needs to be instantiated, eg:
cmd = new SqlCommand ' <- or whatever command object you're using.
or the connection object?
conn = new SqlConnection
Check through yur code to ensure theses objects get instantiated.
Note, the DataReader object does NOT need to be instantiated, as it gets an instance from the cmd.ExecuteReader function.
Hope this helps,
David
its definately not the connection object or the command object because if it was connection object then conn.open will give error and if it was command object then cmd.executereader will give an error,
in all probability it should be the array thats causing the problem
in all probability it should be the array thats causing the problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's what I thought TheLearnedOne. I'll see if I can change it to an Array and not have to change too much code.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ArrayLists allows for casting of the items to an array of a specified type
for instance
ArrayList list = new ArrayList();
list.Add(1);
list.Add(4);
list.Add(200);
int [] numbers = (int [])list.ToArray(typeof(int ));
will get you an array of integers...
pretty handy to know this, one would think!
for instance
ArrayList list = new ArrayList();
list.Add(1);
list.Add(4);
list.Add(200);
int [] numbers = (int [])list.ToArray(typeof(int
will get you an array of integers...
pretty handy to know this, one would think!
Here is a link for a straightforward method of data access:
http://vsnetdatabinding.blogspot.com/
The datatable can be used to create an array or arraylist.
http://vsnetdatabinding.blogspot.com/
The datatable can be used to create an array or arraylist.
ASKER
I had to do it with an ArrayList like TheLearnedOne and b1XML2 suggested.
Aparently using the "new" keyword lets .net automatically assign memory.
Aparently using the "new" keyword lets .net automatically assign memory.
in vb I would normally
redim LinkDesc[20]
or in your case
redim LinkDesc[dtaReader.Rows.Co
(or something like this. Sorry don't have VS running on this computer)
Basically i think the array is not dimensioned with a length. you should be able to test the length before hand. or even assign the array element 0 a string beforehand and see if it will still crash.