Thursday 8 November 2012

execute scalar,execute reader in asp.net




example execute scalar:
con.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from student ", con);
        int res = Convert.ToInt32(cmd.ExecuteScalar());
        Label1.Text = "total no of students :"+res.ToString();
        con.Close();
example execute reader:
con.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO student (sno,firstname,lastname,phone) VALUES ( @sno, @firstname,@lastname,@phone)", con);
        cmd.Parameters.Add("@sno", SqlDbType.Int).Value = TextBox1.Text;
        cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = TextBox2.Text;
        cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = TextBox3.Text;
        cmd.Parameters.Add("@phone", SqlDbType.Int).Value = TextBox4.Text;
         cmd.ExecuteNonQuery();

            Label2.Text = "record successfully inserted";
        con.Close();

No comments:

Post a Comment