Showing posts with label opening. Show all posts
Showing posts with label opening. Show all posts

Sunday, March 11, 2012

A SqlDataReader is returning an int, when it should be returning a tinyint

I am opening a simple command against a view which joins 2 tables, so that I can return a column which is defined as a tinyint in one of the tables. The SELECT looks like this:

SELECT TreatmentStatusFROM vwReferralWithAdmissionDischarge
WHERE ClientNumber = 138238AND CaseNumber = 1AND ProviderNumber = 89

The TreatmentStatus column is a tinyint. When I execute that above SQL SELECT statement in SQL Server Management Studio (I am using SQL Server 2005) I get a value of 2. But when I execute the same SQL SELECT statement as a part of a SqlDataReader and SqlCommand, I get a return data type of integer and a value of 1.

Why?

If you are just reteieving one value you might want to use ExecuteScalar which is faster and has less overhead than ExecuteReader.

|||

Try this:

int numericValue = System.Convert.ToInt32(yourDataReader.GetByte(0));

Cheers

|||

humormuch:

Try this:

int numericValue = System.Convert.ToInt32(yourDataReader.GetByte(0));

Cheers

I forgot to mention, in my original post, that I was using the GetByte() method of the SqlDataReader object, but that raised the following error message:

"Specified cast is not valid"

That's why I brought up the whole thing about knowing that the column was atinyint, but that the SqlDataReader in my ASP.NET 2.0 page is returning aint value instead, which I simply donot understand. Here is the relevant code snippet:

Dim sbAs StringBuilder =New StringBuilder("SELECT TreatmentStatus FROM vwReferralWithAdmissionDischarge ")sb.Append(String.Format("WHERE ClientNumber = {0} ", lClientNumber))sb.Append(String.Format("AND CaseNumber = {0} ", byCaseNumber))sb.Append(String.Format("AND ProviderNumber = {0}", nProviderNumber))Dim cmCheckTreatmentStatusAs SqlCommand =New SqlCommand(sb.ToString(), cn)cmCheckTreatmentStatus.CommandType = CommandType.TextDim sdrCheckTreatmentStatusAs SqlDataReader = cm.ExecuteReader()sdrCheckTreatmentStatus.Read()If sdrCheckTreatmentStatus.IsDBNull(0)Then'NOOPElseDim byTreatmentStatusAs Byte = sdrCheckTreatmentStatus.GetByte(0)'other stuff occurs hereEnd If

It is the line "Dim byTreatmentStatusAs Byte = sdrCheckTreatmentStatus.GetByte(0)" which is raising the error.

|||

Never mind, I found the mistake. It was a stupid mistake on my part. I had created a SqlCommand calledcmCheckTreatmentStatus, and then ran the ExecuteReader() method on another SqlCommand I defined earlier, calledcm.

I'm sorry everyone.

|||

DoctorWho:

Never mind, I found the mistake. It was a stupid mistake on my part. I had created a SqlCommand calledcmCheckTreatmentStatus, and then ran the ExecuteReader() method on another SqlCommand I defined earlier, calledcm.

I'm sorry everyone.

No problem. I remember I have done that quite a few times...

Friday, February 24, 2012

A query runs 1 times slower from a .NET application the from Query

Just a guess.
It might be the delay in creating and opening the connection.
Why don't you log the current time just before calling the SP and after it
and find the time difference. That can narrow down on what the issue is.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/
"Boaz Ben-Porat" wrote:

> Computer: 3.4 Ghz CPU, 1 GB RAM, 2003 Server
> database : MS SqlServer 2000 Enterprise. ~ 10 GB database file. Largest
> table in the database contains 11,000,000 records.
> Framework: .NET 2.0
> I try to run a query against the database, selecting aggregated data from
> views based on the large table.
> When executed from the Query Analizer, it takes 13 seconds.
> When executed from a .NET application, it takes 140 seconds.
> The database is well tuned (or else the query analizer would go slowly), s
o
> I can't find the reason for this difference.
> Any suggestion ?
> TIA
> Boaz Ben-Porat
> Milestone Systems
>
>Thanks for a quick answer.
The time I refer to is after the connection is opened.
the relevant code:
DbDataReader dr = null;
try
{
// This method opens a connection, if not allready opened
Connect();
// dbCommand is an input parameter of type DbCommand. It contains the SQL
statement
dbCommand.Connection = _connection;
DateTime t1 = DateTime.Now;
dr = dbCommand.ExecuteReader();
DateTime t2 = DateTime.Now;
TimeSpan ts = t2 - t1;
int milli = (int)ts.TotalMilliseconds; // milli contains the execution time
of dbCommand.ExecuteReader();
Boaz Ben-Porat
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:49A213DD-227B-4602-81ED-5ADF4E32687E@.microsoft.com...
> Just a guess.
> It might be the delay in creating and opening the connection.
> Why don't you log the current time just before calling the SP and after it
> and find the time difference. That can narrow down on what the issue is.
> --
> -Omnibuzz (The SQL GC)
> http://omnibuzz-sql.blogspot.com/
>
> "Boaz Ben-Porat" wrote:
>

Sunday, February 19, 2012

A problem when opening PDF and EXCEL report

Hi, All
This is really weird. Here is the full story:
My company has a test site to test the application we are developing,
and the Reporting Server is on the same machine. Each developer has the
full application on their own machine so that they can test other parts
individually and check in to the test site later. But we are all using
the same webservice server (that test site machine) and the same report
database.
Now, when I run the report part locally, by locally, I mean I am
running the application in my own machine, but that application is
still loading the remote webservice to generate the report from the
remote database. The report generation is working correctly, I can open
the report and view.
But, if I run that part in the test site, it has a problem. It still
generates a report, but it will prompt a download window, and the
report extension name is always ASPX, (for pdf and excel file). If I
click Open, it will use Visual Studio to open it. If I click Save, and
change the extension name, it's a correct report.
More interesting, for PDF report, if the client machine has Adobe
Reader 7.0, it won't even prompt, instead, it would just show an empty
window, you know those empty windows, you can see the background
through that window, and if you move the window, the background will
follow. Yup, a kind of normal window. It only has that prompt window if
the client has Adobe Reader 6.0
Anyone has a clue how to solve this? Any help is appreciated, thank you
very much!
--
Best Regards!
SowenI saw this during the beta for 2005, but it cleared up once I went with the
released version of 2005. Has this machine ever had the beta on it?
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"sowen" <sowencheung@.gmail.com> wrote in message
news:1139930602.042085.322510@.f14g2000cwb.googlegroups.com...
> Hi, All
> This is really weird. Here is the full story:
> My company has a test site to test the application we are developing,
> and the Reporting Server is on the same machine. Each developer has the
> full application on their own machine so that they can test other parts
> individually and check in to the test site later. But we are all using
> the same webservice server (that test site machine) and the same report
> database.
> Now, when I run the report part locally, by locally, I mean I am
> running the application in my own machine, but that application is
> still loading the remote webservice to generate the report from the
> remote database. The report generation is working correctly, I can open
> the report and view.
> But, if I run that part in the test site, it has a problem. It still
> generates a report, but it will prompt a download window, and the
> report extension name is always ASPX, (for pdf and excel file). If I
> click Open, it will use Visual Studio to open it. If I click Save, and
> change the extension name, it's a correct report.
> More interesting, for PDF report, if the client machine has Adobe
> Reader 7.0, it won't even prompt, instead, it would just show an empty
> window, you know those empty windows, you can see the background
> through that window, and if you move the window, the background will
> follow. Yup, a kind of normal window. It only has that prompt window if
> the client has Adobe Reader 6.0
> Anyone has a clue how to solve this? Any help is appreciated, thank you
> very much!
> --
> Best Regards!
> Sowen
>|||hi, Bruce
thanks for your reply.
But we are still using VS 2003, SQL 2000 and .NET framework 1.1|||Ahh, OK. What Service Pack are you on?
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"sowen" <sowencheung@.gmail.com> wrote in message
news:1139933336.752696.258260@.g44g2000cwa.googlegroups.com...
> hi, Bruce
> thanks for your reply.
> But we are still using VS 2003, SQL 2000 and .NET framework 1.1
>|||for reporting service, SP2
for 2003 server, SP1,|||How are your viewing the report? In your own app? Report Manager? If your
own app are you using URL integration or web services? If in your own app,
does it work properly when you use Report Manager?
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"sowen" <sowencheung@.gmail.com> wrote in message
news:1139934523.097245.13230@.g44g2000cwa.googlegroups.com...
> for reporting service, SP2
> for 2003 server, SP1,
>|||via URL. sth like
http://localhost/mmis/ReportViewer/tabid/140/desktopdefault/+/Default.aspx,
(we are using DNN too.)
the code generating the report is correct.
It works properly if I run all application on my own machine's IIS.
It only has those problems if I run it on our test site (remotely)|||OK, I think what is happening is the Server 2003 internet security. Try
putting in the name of the machine instead of localhost and add the local
machine to the safezone.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"sowen" <sowencheung@.gmail.com> wrote in message
news:1139936364.847940.143500@.g44g2000cwa.googlegroups.com...
> via URL. sth like
> http://localhost/mmis/ReportViewer/tabid/140/desktopdefault/+/Default.aspx,
> (we are using DNN too.)
> the code generating the report is correct.
> It works properly if I run all application on my own machine's IIS.
> It only has those problems if I run it on our test site (remotely)
>|||do you mean adding our test site url in IE security trusted site?|||Here is what I suggest you do. Go to the test server. Open up IE and go to
report manager and make sure your reports and exports etc work as you they
should. Until you know it all works from report manager I wouldn't waste
much time wondering why it isn't working from your app. My guess is that you
will see some of the same issues there. Windows 2003 SP1 really protects
what web sites you can go to. You most likely need to add to trusted sites
in IE on the server.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"sowen" <sowencheung@.gmail.com> wrote in message
news:1139937391.453464.226010@.z14g2000cwz.googlegroups.com...
> do you mean adding our test site url in IE security trusted site?
>|||it works on our test site.
It is frustrated that the same code actually works on our another
remote test site, I just found out.
Is there any security configuration issue I should pay attention to?
It's strange that the same thing working on one machine but not another
machine.|||I mean it works on the test site report manager. I can see everything
running properly. But I have the same problem if I run the ASP.NET
program to create the PDF or EXCEL report, the prompt window asks me to
download a ASPX file.
But if I run the same code on another test machine, there is no such a
problem.
strange...