Converting Object to Short

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweatha
    New Member
    • Mar 2008
    • 44

    Converting Object to Short

    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
    [code=c#]
    protected void Button3_Click(o bject sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(" user id=sa;password= cast;database=H ello_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.Tex t + "'";
    //and count(Date)>=5" ;

    Int16 cnt;
    cnt = cmd.ExecuteScal ar();
    if (cnt >= 5)
    {
    Response.Write( "Plz select another appointment date");
    }
    else
    {

    String sql = "insert into Appointment values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DateTextBox.Tex t + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "')";
    SqlCommand cmd1 = new SqlCommand(sql, con);
    cmd1.ExecuteNon Query();
    }
    }
    [/code]
    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\Suga nya_Hello_Dr\Ap pointment.aspx. cs 93 15 E:\suganya\Suga nya_Hello_Dr\
    Last edited by Frinavale; Feb 23 '09, 05:12 PM. Reason: Moved to ASP.NET Answers from .NET (Plater added [code] tags)
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If an explicit conversion exists (as it says), you can type-cast it with (short)

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      Tried with int but failed

      I tried type-casting the return value for ExecuteScalar() to int in the following code:

      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:

      Code:
      int off = Convert.ToInt32(
                      FormStaffManager.Instance.CheckExistingStaff(prop));
                  return off;
      Anyone knows how I can convert object to int successfully?

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        This should work:

        Code:
        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:

        Code:
        // set a breakpoint after this line
        object result = cmd.ExecuteScalar();

        Comment

        • mbewers1
          New Member
          • Feb 2009
          • 68

          #5
          thanks, that worked a treat.

          So did this:

          int result = command.Execute NonQuery();
          return result;

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

          thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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.....

            Code:
            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.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              ExecuteScalar returns an object, and C# requires explicit casting, so you would have to cast your return value to the proper type.

              Comment

              Working...