TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
spring roast
NA
4
0
ExecuteScalar returns null.
Nov 1 2006 10:43 PM
Hi, i am facing a problem. i want to retrieve a query result from my database, but my query happeneds to return null, but i wrote a method using ExecuteScalar to retrive the value, but if the qurey return null, it will be caught by the catch (Exception ex){} part. How should i go about it? could someone please advice me?
Code:
// execute a select command and return a single result as a string
public
static
string
ExecuteScalar(DbCommand command)
{
// The value to be returned
string
value =
""
;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the connection of the command
command.Connection.Open();
// Execute the command and get the number of affected rows
value = command.ExecuteScalar().ToString();
}
catch
(Exception ex)
{
//Log eventual errors and rethrow them
throw
ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
// return the result
return
value;
}
However i got another method which does select query that returns a datatable. But i don't know how retrieve a row out from a datatable, For example using a datareader will be like dreader[0].ToString. But in datatable's case how should i do that? Can someone please advice me thanks..
Code:
public
static
DataTable ExecuteSelectCommand(DbCommand command)
{
// The DataTable to be returned
DataTable table;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the data connection
command.Connection.Open();
// Execute the command and save the results in a DataTable
DbDataReader reader = command.ExecuteReader();
table =
new
DataTable();
table.Load(reader);
// Close the reader
reader.Close();
}
catch
(Exception ex)
{
//Utilities.SendErrorLogEmail(ex);
throw
ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
return
table;
}
Reply
Answers (
1
)
Login to CGI site with autnentication
Read Cookie value containg special characters in asp.net