0
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{

   SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
    cmd.Connection = con;
    con.Open();
    dr = cmd.ExecuteReader();
    dr.Read();
    d = dr[0].ToString();
   //d =(string) Label4.Text;
    con.Close();

  }

I want integer value from database

2
  • use: int d = dr.GetInt32[0]; also check: stackoverflow.com/questions/7388475/… Commented Jan 4, 2014 at 11:36
  • If you want, then why don't you just do it: ∫i=Convert.ToInt32(dr[0];int i = Convert.ToInt32(dr[0]); Commented Jan 4, 2014 at 11:37

3 Answers 3

3

try

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int d = dr.GetInt32(0);
con.Close();

dr.GetInt32(0) should read an int at position 0

Sign up to request clarification or add additional context in comments.

1 Comment

Please use the parameters ("select FromYear from Factory where Cal_ID=@param) avoiding string concatenation: SqlCommand with Parameters
1
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{

   SqlCommand cmd = new SqlCommand();
   cmd.CommandType = CommandType.Text;
   cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
   cmd.Connection = con;
   con.Open();

   d = GetYear(cmd).ToString();

   con.Close();
}

the "dirty" work is done by GetYear :

private const DEFAULT_YEAR = 2000;
private int GetYear(System.Data.IDbCommand command) {
   int year = DEFAULT_YEAR;
   using (System.Data.IDataReader reader = command.ExecuteReader()) {
      if (reader.Read()) {
         year = GetIntOrDefault(reader, 0);
      }
   }
   return year;
}

private  int GetIntOrDefault(System.Data.IDataRecord record, int ordinal) {
   if (record.IsDBNull(ordinal)) {
      return DEFAULT_YEAR;
   } else {
      return record.GetInt32(ordinal);
   }
}

Comments

0
string InvAmountQuery = "SELECT InvAmount FROM Registration WHERE Email='" + useremail + "' ";
SqlCommand InvAmountcom = new SqlCommand(InvAmountQuery, conn);
int InvAmount = Convert.ToInt32(InvAmountcom.ExecuteScalar().ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.