473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Data Reader Question......

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


Nov 18 '05 #1
2 1118

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

"Ankur Smith" wrote:
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


Nov 18 '05 #2
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

"Ethem Azun" <Et*******@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...

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

"Ankur Smith" wrote:
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


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: sabarish | last post by:
Hai to all the people. please tell how can i Connect to the Database through C program. I have No clarity in that.
1
by: Mike P | last post by:
I'm using 1 connection to open 3 separate data readers to read data into 3 data grids. However, when I try to use the 2nd reader it says that the first reader must be closed. So I use Close() to...
6
by: Natan Vivo | last post by:
I had to built a custom data paging control for my asp.net app and SQL Server. This is what I'm doing: 1. do a SELECT to a SqlDataReader 2. create a DataTable with data from GetSchema() 3....
2
by: Chucker | last post by:
Hi Community, I think I can store Binary Data in SQL Server but when I try to retrieve it, I always only get one byte. I think I stored my Binary Data in SQL Server in a Colum of Type Image....
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
2
by: Joe Campbell | last post by:
I have a DBA that wrote a stored procedure that does a SELECT from a particluar SQL Server table. Within that stored procedure he links over to grab a column from another database table. I need to...
4
by: Derek Hart | last post by:
For testing purposes, I need some code that will loop through all attributes and elements in an XML file, and replace the data with the element or attribute name? Does anybody have code that will...
1
by: weird0 | last post by:
I have created a GridView and dynamically added data to it by creating a DataTable(as advised) and bound it with a reader. Then, assigned the DataSource of GridView to DataTable. But even that aint...
1
by: dotnetnovice | last post by:
Hi everybody... I want to show data in the data gridview placed in my form after getting data from my data access layer class through a class... Here is my code in the data access layer class....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.