473,395 Members | 1,456 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.

Converting Object to Short

44
Hi

I have to select count(Date) from Appointment table having count<5. If count(Date)<5 then I should insert the data from my page. For that I have given the coding as
Expand|Select|Wrap|Line Numbers
  1. protected void Button3_Click(object sender, EventArgs e)
  2.     {
  3.         SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AURORA-SERVER;");
  4.         con.Open();
  5.         SqlDataReader rd;
  6.         SqlCommand cmd = new SqlCommand();
  7.         cmd.Connection = con;
  8.         cmd.CommandText = "select count(Date) from Appointment where Date='" + DateTextBox.Text + "'"; 
  9.         //and count(Date)>=5";
  10.  
  11.         Int16 cnt;
  12.         cnt = cmd.ExecuteScalar();
  13.         if (cnt >= 5)
  14.         {
  15.             Response.Write("Plz select another appointment date");
  16.         }
  17.         else
  18.         {
  19.  
  20.             String sql = "insert into Appointment values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DateTextBox.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "')";
  21.             SqlCommand cmd1 = new SqlCommand(sql, con);
  22.             cmd1.ExecuteNonQuery();
  23.         }
  24.       }
  25.  
But when I run, I am getting the following error.


Error 1 Cannot implicitly convert type 'object' to 'short'. An explicit conversion exists (are you missing a cast?) E:\suganya\Suganya_Hello_Dr\Appointment.aspx.cs 93 15 E:\suganya\Suganya_Hello_Dr\
Jul 1 '08 #1
6 12858
Plater
7,872 Expert 4TB
If an explicit conversion exists (as it says), you can type-cast it with (short)
Jul 1 '08 #2
I tried type-casting the return value for ExecuteScalar() to int in the following code:

Expand|Select|Wrap|Line Numbers
  1. using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
  2.             {
  3.                 sqlCon.Open();
  4.                 SqlCommand command = sqlCon.CreateCommand();
  5.                 command.CommandType = CommandType.Text;
  6.                 command.CommandText = @"SELECT COUNT(*) 
  7.                     FROM Staff WHERE Phone = @phone AND Email = @email";
  8.  
  9.                 command.Parameters.Add("@phone", SqlDbType.Char, 9).Value =
  10.                     prop.Phone;
  11.                 command.Parameters.Add("@email", SqlDbType.VarChar, 50).Value =
  12.                     prop.Email;
  13.                 return command.ExecuteScalar();
  14.             }
  15.  
but when I try to access the return value, using an int variable in my code-behind page for a Windows Form, I get a result of 0 every time. I've tried applying the query in SQL Query Analyser and I've got > 0 back, so I know it works.

I've also tried using Convert.ToInt32 and that didn't work either:

Expand|Select|Wrap|Line Numbers
  1. int off = Convert.ToInt32(
  2.                 FormStaffManager.Instance.CheckExistingStaff(prop));
  3.             return off;
  4.  
Anyone knows how I can convert object to int successfully?
Feb 22 '09 #3
vekipeki
229 Expert 100+
This should work:

Expand|Select|Wrap|Line Numbers
  1. int cnt = (int)cmd.ExecuteScalar();
If you are getting 0, then your SqlCommand actually returns 0. Try to add this and set a breakpoint to check the actual object value:

Expand|Select|Wrap|Line Numbers
  1. // set a breakpoint after this line
  2. object result = cmd.ExecuteScalar();
Feb 23 '09 #4
thanks, that worked a treat.

So did this:

int result = command.ExecuteNonQuery();
return result;

.. as I needed to return a value in the method.

thanks
Feb 23 '09 #5
Frinavale
9,735 Expert Mod 8TB
Um, I think the most obvious thing here is the ExecuteScalar() method is returning a Short, and therefor your "cnt" variable should be of type Short.....

Expand|Select|Wrap|Line Numbers
  1. Short cnt;
  2. cnt = cmd.ExecuteScalar();
There's really no reason why casting should be used here since you know the type is a Short.


The other thing is that you shouldn't be using the Response.Write() method like you are. See this post for more details on why.
Feb 23 '09 #6
Curtis Rutland
3,256 Expert 2GB
ExecuteScalar returns an object, and C# requires explicit casting, so you would have to cast your return value to the proper type.
Feb 23 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ken Godee | last post by:
I'm confused, I have a class returning a value from a method of that class and I'm getting a reference to the object instead of the actual returned value. How can I convert it back to an actual...
4
by: Svetoslav Vasilev | last post by:
Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte. The field values in a DataTable as we all know are returned...
3
by: DDE | last post by:
Hi all, I have defined a meththod supposed to do some treatment with the class it receives as argument. This method can receive different type of classes so it's argument is defined as an...
1
by: santhi | last post by:
Hi, Pls Anyone guide me how to convert object into byte. I'm storing the Byte with id in the hashtable and while retrieving i'm trying to put the Hashtable value into Byte back.But i'm not...
1
by: bryja_klaudiusz[at]poczta[dot]fm | last post by:
Hi, How to convert object to string? I try procedure like this (when I have byte array I convert it to string): public byte ObjectToByteArray(Object obj) { MemoryStream fs = new...
6
by: Martin Carolan | last post by:
Hi there, I have an interface (called AuthenticationInterface) shown below: Public Interface AuthenticationInterface Function IsLoggedIn() As Boolean Function Authenticate(ByVal User As...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
3
by: Joe Van Dyk | last post by:
I have a class that is a wrapper around a double. Is there a way to automatically convert an object of that class to a double whenever a double is wanted? Joe
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.