Monday 5 August 2013

Cannot implicitly convert type 'decimal' to 'string' {Solved}

Error Page:

protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=JISHNU\\SQLEXPRESS;Initial Catalog=dreamtheweb;Integrated Security=True");
            con.Open();
            SqlCommand phone = new SqlCommand("select phone from account where id='1' ", con);
            decimal phone1 = (decimal)phone.ExecuteScalar();
            Label1.Text = phone1;
         }

Reason: This type of error detected due to absence conversion of data type .
Solution: Use Convert.ToDatatype(variable);  
ie

   protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=JISHNU\\SQLEXPRESS;Initial Catalog=dreamtheweb;Integrated Security=True");
          
            SqlCommand phone = new SqlCommand("select phone from account where id='1' ", con);
            con.Open();
            decimal ph = (decimal)phone.ExecuteScalar();
            Label1.Text = Convert.ToString(ph);
            con.Close();
        
        }

0 comments:

Post a Comment