Best practice

G

Guest

Hello All,

I have a question related to performance and/or best practice.

Whenever I try to access a column's value from the dataset I use the
following convention.

if(objData.Tables[0].Rows[0]["FIELD_ID"].ToString()!="")
LINE 1
{
strLength = objData.Tables[0].Rows[0]["FIELD_ID"].ToString(); LINE 3
}

In the above code, in LINE 1 I am checking if that column is empty or not
and if it is not empty then I am assiging it to another variable by accessing
the column from dataset in LINE3

Can I optimize my code further. Am I coding the right way? I feel that
accessing the dataset object twice somehow is an expensive and redundant
operation.

Any pointers?

Thanks!!
 
K

Karl Seguin [MVP]

Well, if you are accessing indexes often, it's often worthwhile to save in a
local variable. Also,not sure why an empy string is to be avoided in your
case....normally you see the kind of thing you are doing with DBNull, not
empty string:

DataRow row = objData.Tables[0].Rows[0];
string name;
if (row["col"] != DBNull.Value)
{
name = Convert.ToString(row["col"]);
}


you can use whatever to get the string out ToString, or Convert.ToString, or
cstr() (vb.net), doesn't really matter.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top