Monday 1 April 2013

ExecuteScalar(); in Ado.Net

Hello Friends !! Today I am just going to explain the concept about ExecuteScalar(); in ado.net which is given below:
ExecuteScalar !!

ExecuteScalar (); 
                  This method is also used for execute Sql Statement but it return only single value. When the select statement executed by ExecuteScalar () method then returns a row and multiple rows, then the method will return the value of first column of first row returned by the query and other column and row ignore by this method.The code  screen and example of the ExecuteScalar as given below:

Source code:

using System.Windows.Forms;
using System.Data.SqlClient;

namespace executescaler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.;initial catalog=project2;integrated security=true");
            con.Open();
         
            SqlCommand cmd = new SqlCommand("select fullname from newuser where id='"+textBox1.Text+"'",con);
            label2.Text = cmd.ExecuteScalar().ToString();
            con.Close();

        }
    }
}

Output Screen:


 I hope this post will help you ..
If you've some query regarding to this article then post your comment.your comment will be appropriate.

0 comments:

Post a Comment

Hey thanax alot to comment i will revert you back soon...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites