|
|
Is there a way to find out if a query will return data before it is made?
I have an unusual problem. My code uses a 3rd party dll to make database calls from my silverlight client to a sql server database. In my code, I make dozens of calls one after the other. All of the calls execute successfully. But sometimes the result of a successful call is an empty dataset. Is there any way I can modify my call so that in the return will tell me what call was made that returned the empty dataset?
The 3rd party software pretty much retruns whatever command I give as a query I test in Microsoft Server Management Studio. Is there some sort of SQL command that will test if some sort of SELECT statement will return data and can that return statement contain the ID's or parameters used to make the statement?
|
|
|
|
|
First, the subject line is for a short description of your problem/question, not for the question itself. Please edit and shorten.
Xarzu wrote: Is there some sort of SQL command that will test if some sort of SELECT statement will return data
SELECT COUNT(*)...
Xarzu wrote: The 3rd party software pretty much returns whatever command I give as a query
Xarzu wrote: Is there any way I can modify my call so that in the return will tell me what call was made that returned the empty dataset?
You are passing the command to a method and you need that method to tell you what it was you passed to it?
No comment
|
|
|
|
|
SELECT COUNT(1) ... is more performant.
Even so, it will still perform the query, but won't return any data but the count. So, it will only be "faster" because it doesn't have to gather-up the data to send back over the wire. The resulting data still has to be produced in order to count it.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
Xarzu wrote: Is there a way to find out if a query will return data before it is made?
Nope. Doing a COUNT will execute the query and count the results.
Xarzu wrote: I have an unusual problem. My code uses a 3rd party dll to make database calls from my silverlight client to a sql server database.
Why? What does it add/why is it required?
Xarzu wrote: In my code, I make dozens of calls one after the other. All of the calls execute successfully. But sometimes the result of a successful call is an empty dataset. Is there any way I can modify my call so that in the return will tell me what call was made that returned the empty dataset?
The method that makes the call usually also returns the dataset. Where are you "receiving" your datasets? Over an event ?
Xarzu wrote: The 3rd party software pretty much retruns whatever command I give as a query I test in Microsoft Server Management Studio. Is there some sort of SQL command that will test if some sort of SELECT statement will return data and can that return statement contain the ID's or parameters used to make the statement?
Nope. If you want to diagnose your Sql as it is executed (with the parameters) then you'd use a Profiler[^].
Bastard Programmer from Hell
|
|
|
|
|
Hi All,
I have a routine that calls a C++/CLI DLL from C# in order to generate an image. After the generation, I have (in the DLL) a pointer to 'unsigned short' array and the image dimensions. Now I'd like to somehow use this pointer to display the image in a C# form (using PictureBox ot ony other suitable component), but I have no idea how...
Thanks for the help,
Eyal.
|
|
|
|
|
Create a Bitmap of the right size with the appropriate pixel depth (not sure what that is for ushort, you might have to convert on the fly), call LockBits to get the address of its data, and copy the data from the C array into that one (using unsafe code or Marshal methods, depending on your requirements).
|
|
|
|
|
you might be lucky and find the solution here[^]; make sure to read all of it though.
|
|
|
|
|
Greetings Gurus,
While enumerating the drives in my machine I am trying to get the output to print "No Label" if the VolumeLabel value is null / length = 0.
When I run the app, after the C: and D: respond, I keep getting an exception "the device is not ready" if I use the "No Label" qualifier, but the code runs fine with that bit commented out.
What syntax should I be using please?
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.VolumeLabel.Length == 0)
d.VolumeLabel = "No Label";
string label = d.VolumeLabel;
if (d.IsReady == true)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.AvailableFreeSpace / 1024000000);
Console.WriteLine(d.TotalFreeSpace / 1024000000);
Console.WriteLine(d.TotalSize / 1024000000);
}
}
|
|
|
|
|
My bad, order was wrong. Should read
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
if (d.IsReady == true)
{
string label = d.VolumeLabel;
if (label.Length == 0)
d.VolumeLabel = "No Label";
{
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.AvailableFreeSpace / 1024000000);
Console.WriteLine(d.TotalSize / 1024000000);
}
}
}
|
|
|
|
|
Try this instead:
string label = d.VolumeLabel;
if (label.Length == 0)
label = "No Label";
{
Console.WriteLine(label);
What was the point of assigning to label if you weren't going to use it for anything other than a lenght test?
|
|
|
|
|
Of course you're getting that exception if you ask the volume label of a drive that doesn't have any media inserted like a DVD, CD, BlueRay drive for instance:
DriveInfo[] allDrives = DriveInfo.GetDrives();
long divisorForGiga = 1024 * 1024 * 1024;
foreach (DriveInfo d in allDrives)
{
String label = d.VolumeLabel;
if (d.IsReady == true)
{
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0)
{
d.VolumeLabel = "No Label";
}
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.AvailableFreeSpace / divisorForGiga);
Console.WriteLine(d.TotalFreeSpace / divisorForGiga);
Console.WriteLine(d.TotalSize / divisorForGiga);
}
else
{
Console.WriteLine("Drive {0}: is not ready! Maybe there's no media inserted.")
}
}
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
I'm going to change your life (just like mine was a few years back...)
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) {
Can be written as this:
if (string.IsNullOrEmpty(d.VolumeLabel)) {
|
|
|
|
|
And that nice new version
if (string.IsNullOrWhitespace(" "))
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
string sayii = textBox1.Text.Substring(0, textBox1.Text.Length);
uint[] sayicc = new uint[textBox1.Text.Length];
for (int i = 0; i < textBox1.Text.Length; i++)
{
sayicc[i] = Convert.ToUInt32(sayii[i] - 48);
}
uint[] dizi2 = new uint[textBox1.Text.Length];
uint bit = 1;
for (int j = 0; j < textBox1.Text.Length; j++)
{
dizi2[j] = bit;
bit *= 2;
}
Array.Reverse(dizi2);
uint deger = 0;
for (int i = 0; i <= dizi2.Length - 1; i++)
{
dizi2[i] *= sayicc[i];
if (sayicc[i] == 1)
{
deger += dizi2[i];
}
}
textBox2.Text = " " + deger.ToString();
}
else
{
MessageBox.Show("Deger girilecek alan boş.", "HATA!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
|
|
|
|
|
Why not this?
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox2.Text = Convert.ToInt32(textBox1.Text, 2).ToString();
}
else
{
MessageBox.Show("Deger girilecek alan boş.", "HATA!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
This code for binary to decimal. I didint see like this easy code. The other calculating codes are too long and complex. I made this and you can use easily.
|
|
|
|
|
Hello Peter,
your code is excelent , but have no control of what user enters, except if it is an emty string,
so I shall use your code with small change in that direction.
void Button1Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
try
{
textBox2.Text = Convert.ToInt32(textBox1.Text, 2).ToString();
}
catch (Exception)
{
MessageBox.Show("You didn't entered a Binary number !",
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You didn't entered a Binary number !",
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
|
|
|
|
|
Indeed your code is better for the real world. My point was about (badly) reinventing the string-to-number conversion.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
You know if I saw a piece of code that said binary to decimal, and if I said 1001, I'd expect to get 9 back instead of 1001! You would be in a world of hurt if I entered an 11 digit "binary" number and that is only slightly longer than 1/3 of a 32 bit int.
|
|
|
|
|
If you are saying what I think you are saying, then you need to take a close look at the second argument in the call to Convert.ToInt32() . It wouldn't be me that hurt.
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
True, didn't even notice you were using an overload I didn't know about.
|
|
|
|
|
Hello mehmetali,
peter have excelent solution ,
I made mine own which is not as elegant.
It is console application made in SharpDevelop C#.
This code is update of old one, it have no problem with entry of numbers as old version,
now it works with float point numbers and it folows full math logic.
using System;
namespace Binary_to_Decimal
{
class Program
{
public static void Main(string[] args)
{
string [] DecimalFloat = new string[2];
string Binary = "";
bool RealRelevance = false;
bool BinaryRelevance = false;
int Index = 0;
int Lenght = 0;
int Exponent = 0;
int Cipher = 0;
double BaseExponent = 0;
double RealNumber = 0;
double D = 0;
double F = 0;
string Negative = "";
string DecimalPart = "";
string FloatPart = "";
string Letter = "";
begining :
Console.Title = "Program Binary to Real ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Binary to Real");
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Enter binary number (exmpl: 1001) : ");
Binary = "";
Binary = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
RealRelevance = false;
RealNumber = 0;
RealRelevance = double.TryParse(Binary,out RealNumber);
if (RealRelevance == true)
{
if (Binary.Contains("-"))
{
Binary = Binary.TrimStart('-');
Negative = "- ";
RealNumber = -1 * RealNumber;
}
else if (!Binary.Contains("-") && !Binary.Contains("+"))
{
Negative = "";
}
else if (Binary.Contains("+"))
{
Binary = Binary.TrimStart('+');
Negative = "+ ";
RealNumber = double.Parse(Binary.ToString());
}
if ( Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
DecimalPart = DecimalPart.TrimStart('0');
if (DecimalPart =="")
{
DecimalPart = "0";
}
FloatPart = FloatPart.TrimEnd('0');
if (FloatPart =="")
{
FloatPart = "0";
}
if (DecimalPart =="0" && FloatPart =="0")
{
Binary = "0";
}
if (DecimalPart !="0" && FloatPart =="0")
{
Binary = DecimalPart;
}
if (DecimalPart =="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
if (DecimalPart !="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
}
else
{
Binary = Binary.TrimStart('0');
if (Binary =="")
{
Binary = "0";
}
}
}
else if (RealRelevance == false)
{
Console.WriteLine("Error , you didn't entered a number at all..");
Console.WriteLine();
goto end;
}
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for (Index=0;Index<Lenght ;Index = Index + 1)
{
Letter = Binary.Substring(Index,1);
if (Letter != "0" && Letter != "1" && Letter !=",")
{
BinaryRelevance = false;
Index = Lenght;
}
else
{
BinaryRelevance = true;
}
}
if (BinaryRelevance != true)
{
Console.WriteLine();
Console.WriteLine("Error , you didn't enter binary number ...");
Console.WriteLine();
goto end;
}
if (Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
D = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = DecimalPart.Substring(Index,1);
Cipher = int.Parse(Letter.ToString());
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
F = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = FloatPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Index+1;
Letter = FloatPart.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,-Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
F = F + (Cipher * BaseExponent);
}
RealNumber = D + F;
goto write;
}
else
{
Exponent = 0;
Cipher = 0;
RealNumber = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = Binary.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
RealNumber = RealNumber + (Cipher * BaseExponent);
}
}
write:
if (RealNumber == 0)
{
Negative ="";
}
Console.WriteLine();
Console.WriteLine("Binary number : " + Negative + Binary);
Console.WriteLine();
Console.WriteLine("Real number : " + Negative + RealNumber.ToString());
Console.WriteLine();
end:
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Do you want to run program again y / n : ");
Letter = Console.ReadLine();
if ( Letter == "Y" || Letter == "y")
{
goto begining;
}
}
}
}
Code was updated 09/02/2012.
Whole Solution for IDE SharpDevelop you can download from the bottom of this page :
Conversion from one to another number system [ Open in new window ]
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
modified 26-Feb-12 8:50am.
|
|
|
|
|
Peter's result would put "101" out as " 101" instead of " 5" Sorry, I'm wrong about that. didn't notice or know about the overload.
I still like the following logic a little better, it only allows int results, has more error checking, and gives immediate results as you type. (Change your event delegate)
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool isvalid = true;
string txt1="", txt2 = "HATA!!!";
string sayii = textBox1.Text;
int strt = 0;
int bitOffset = 1;
byte x;
if (sayii.Length == 0)
{
txt1 = "Deger girilecek alan boş.";
isvalid = false;
}
else if (sayii.Length > 32)
{
txt1 = "A binary integer can only be 32 bits long";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(0, 32);
return;
}
else if (sayii.Length == 32)
{
x = (byte)sayii[0];
if (x < 48 || x > 49)
{
txt1 = "A binary value must be 1 or 0";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(1, 31);
return;
}
if (x == 49) strt = int.MinValue;
sayii = sayii.Substring(1, 31);
}
for (int i = sayii.Length; i > 0 && isvalid; )
{
x = (byte)sayii[--i];
if (x == 49)
strt += bitOffset;
else if (x != 48)
{
txt1 = "A binary value must be 1 or 0";
isvalid = false;
}
bitOffset *= 2;
}
if (!isvalid)
{
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else textBox2.Text = " " + strt.ToString();
}
modified 10-Oct-11 21:09pm.
|
|
|
|
|
See this reply[^] above. The same comment applies here.
Also, have a look at this thread[^].
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|