Data Reader Question......

  • Thread starter Thread starter Ankur Smith
  • Start date Start date
A

Ankur Smith

Here is my code.

Dim dtrAct As OdbcDataReader 'Line #1

dtrAct = cmdAct.ExecuteReader() 'Line #2

If Not dtrAct.IsDBNull(dtrAct.GetOrdinal("ACCOUNTNO")) Then 'Line #3

txtActNo.Text = dtrAct("ACCOUNTNO").ToString() 'Line #4

Else

txtActNo.Text = "ErrorData" 'Line #5

End If

To improve the performce is there any way to improve the code in line #3?

Is there any other suggestions in the above statement.

I am using .Net Framework 1.1.

Thanks

Ankur
 
Hi Ankur,

Since you need only one variable value, you don't have to open a reader.

Use cmdAct.ExecuteScalar() instead, which will return a scalar value. You
will have to cast it to the necessary type (in your case to a string.). Put
this into a try catch finally block. Alternative C# implementation would
similiar to (sorry, I'm not very competent in VB)

sqlConnection.Open();
try
{
txtActNo.Text = (string) cmdAct.ExecuteScalar();
}
catch
{
txtActNo.Text = "ErrorData";
}
finally
{
sqlConnection.Close();
}

Hope this helps,

Ethem Azun
 
hi Ethem,



Like below I am reading more than one column.

Can any one suggest me about this?

I know I can avoid NOT statement below by switching TRUE and FALSE
statements.

I am curious is there any efficient statement for line # 3?

Thanks,

Ankur



Dim dtrAct As OdbcDataReader 'Line #1

dtrAct = cmdAct.ExecuteReader() 'Line #2

If Not dtrAct.IsDBNull(dtrAct.GetOrdinal("ACCOUNTNO")) Then 'Line #3
txtActNo.Text = dtrAct("ACCOUNTNO").ToString() 'Line #4
Else
txtActNo.Text = "ErrorData" 'Line #5
End If
If Not dtrAct.IsDBNull(dtrAct.GetOrdinal("ACCOUNTNAME")) Then 'Line #3
txtActName.Text = dtrAct("ACCOUNTNAME").ToString() 'Line #4
Else
txtActName.Text = "ErrorData" 'Line #5
End If
 
Back
Top