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 -
protected void Button3_Click(object sender, EventArgs e)
-
{
-
SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AURORA-SERVER;");
-
con.Open();
-
SqlDataReader rd;
-
SqlCommand cmd = new SqlCommand();
-
cmd.Connection = con;
-
cmd.CommandText = "select count(Date) from Appointment where Date='" + DateTextBox.Text + "'";
-
//and count(Date)>=5";
-
-
Int16 cnt;
-
cnt = cmd.ExecuteScalar();
-
if (cnt >= 5)
-
{
-
Response.Write("Plz select another appointment date");
-
}
-
else
-
{
-
-
String sql = "insert into Appointment values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DateTextBox.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "')";
-
SqlCommand cmd1 = new SqlCommand(sql, con);
-
cmd1.ExecuteNonQuery();
-
}
-
}
-
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\
6 12858
If an explicit conversion exists (as it says), you can type-cast it with (short)
I tried type-casting the return value for ExecuteScalar() to int in the following code: -
using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
-
{
-
sqlCon.Open();
-
SqlCommand command = sqlCon.CreateCommand();
-
command.CommandType = CommandType.Text;
-
command.CommandText = @"SELECT COUNT(*)
-
FROM Staff WHERE Phone = @phone AND Email = @email";
-
-
command.Parameters.Add("@phone", SqlDbType.Char, 9).Value =
-
prop.Phone;
-
command.Parameters.Add("@email", SqlDbType.VarChar, 50).Value =
-
prop.Email;
-
return command.ExecuteScalar();
-
}
-
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: -
int off = Convert.ToInt32(
-
FormStaffManager.Instance.CheckExistingStaff(prop));
-
return off;
-
Anyone knows how I can convert object to int successfully?
This should work: - 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: - // set a breakpoint after this line
-
object result = cmd.ExecuteScalar();
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
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..... -
Short cnt;
-
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.
ExecuteScalar returns an object, and C# requires explicit casting, so you would have to cast your return value to the proper type.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Wallace |
last post by:
Hai,
Can anyone tell how to convert an object into string?
Please help me.... urgent..
Thanx in advance...
|
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...
|
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
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |