ADO.NET output type question

Q.  You are a Web developer for CareerRide. The data is stored in a Microsoft SQL Server 2005 database on a server named CareerPC and the Database name is TestDB. There is one GridView control on the page that you want to fill with table name Employee. Suppose that SqlConnection object is conObj and SqlCommand Object object is cmdObj. Which important properties of command object you will initialize to achieve this task?
- Published on 28 Jul 15

a. cmdObj.CommandType = Text;
 cmdObj.Connection = conObj;
 cmdObj.CommandText = "select * from Employee";
b. cmdObj.CommandConnection = conObj;
 cmdObj.CommandText = "select * from Employee";
c. cmdObj.CommandType = CommandType.Text;
 cmdObj.Connection = conObj;
 cmdObj.CommandText = "select * from Employee";
d. None of the above.

ANSWER: cmdObj.CommandType = CommandType.Text;
 cmdObj.Connection = conObj;
 cmdObj.CommandText = "select * from Employee";
 

    Discussion

  • Ramesh   -Posted on 19 Oct 15
    Write the code in button click event.
    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {

    SqlConnection conObj = new SqlConnection();
    conObj.ConnectionString = "data source=(local);initial catalog=Emp;integrated security=true";
    conObj.Open();
    SqlCommand cmdObj = new SqlCommand();
    cmdObj.Connection = conObj;
    cmdObj.CommandText = "SELECT * FROM Employee";
    SqlDataReader dr = cmdObj.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();

    }

    catch (Exception ex)
    {

    Label1.Text=ex.Message;

    }
    }

Post your comment / Share knowledge


Enter the code shown above:
 
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)