Monday, 24 September 2012

ExecuteReader ExecuteNonQuery ExecuteScalar Examples in Asp.NET when to use what to use in C#.NET,VB.NET


Introduction:

In this article I will explain executereader executenonquery executescalar examples when to use what in asp.net using C#.net and VB.NET.

Description:

If you want to know which command object needs to be used in different situations for that you need to check this article

Differences between ExecuteNonQuery, ExecuteReader and ExecuteScalar in asp.net

If you want to know about each sqlcommand object check below articles

ExecuteNonQuery

ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations. For more details check this article

ExecuteNonQuery Example in asp.net using C#, VB.NET

ExecuteScalar

Execute Scalar will return first row first column value i.e. it will return single value and ignore other values on execution of SQL Query or Stored procedure using command object.
For more details check this article

ExecuteScalar Example in asp.net using C#, VB.NET

ExecuteReader

Execute Reader will be used to return the set of rows, on execution of SQL Query or Stored procedure using command object. For more details check this article

ExecuteReader Example in asp.net using C#, VB.NET

ASP.NET


Introduction:

In this article I will explain how to pass a table valued parameter to stored procedure in asp.net using C#, VB.NET.

Description:

In previous post I explained Pass table as parameter to stored procedure in SQL Server. Now I will explain how to pass a table valued parameter to stored procedure in asp.net using C#, VB.NET.

Before implement this concept first we need to create table type parameter and stored procedure to accept table type as parameter in database for that check this article

Pass table as parameter to stored procedure in SQL Server 2008  

Once stored procedure created with table type parameter in database write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>pass table as parameter to stored procedure in asp.net using c#,vb.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" onclick="btnSubmit_Click" /><br />
<asp:Label ID="lblDetails" runat="server" />
</div>
</form>
</body>
</html>
Now add the following namespaces in code behind

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
After add namespaces write the following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// New Table with data
DataTable dt=new DataTable();
dt = new DataTable("EmpDetails");
dt.Columns.Add("EmployeeId", typeof(int));
dt.Columns.Add("EmployeeName", typeof(string));
dt.Columns.Add("EmpRole", typeof(string));
dt.Rows.Add(2,"Mahesh","Developer");
dt.Rows.Add(3,"Prasanthi","Consultant");
dt.Rows.Add(4,"Madhav","Analyst");
dt.Rows.Add(5,"Nagaraju","Developer");
// Send datatable as parameter to stored procedure
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("prc_InsertEmpDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tblvaluetype=  cmd.Parameters.AddWithValue("@TVP", dt);  //Passing table value parameter
tblvaluetype.SqlDbType = SqlDbType.Structured; // This one is used to tell ADO.NET we are passing Table value Parameter
int result = cmd.ExecuteNonQuery();
if (result >= 1)
{
lblDetails.Text = result+" Rows Inserted into table ";
}
else
{
lblDetails.Text = "No Rows Inserted into table ";
}
con.Close();
}
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
' New Table with data
Dim dt As New DataTable()
dt = New DataTable("EmpDetails")
dt.Columns.Add("EmployeeId", GetType(Integer))
dt.Columns.Add("EmployeeName", GetType(String))
dt.Columns.Add("EmpRole", GetType(String))
dt.Rows.Add(2, "Mahesh", "Developer")
dt.Rows.Add(3, "Prasanthi", "Consultant")
dt.Rows.Add(4, "Madhav", "Analyst")
dt.Rows.Add(5, "Nagaraju", "Developer")
' Send datatable as parameter to stored procedure
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("prc_InsertEmpDetails", con)
cmd.CommandType = CommandType.StoredProcedure
Dim tblvaluetype As SqlParameter = cmd.Parameters.AddWithValue("@TVP", dt)
'Passing table value parameter
tblvaluetype.SqlDbType = SqlDbType.Structured
' This one is used to tell ADO.NET we are passing Table value Parameter
Dim result As Integer = cmd.ExecuteNonQuery()
If result >= 1 Then
lblDetails.Text = result+" Rows Inserted into table "
Else
lblDetails.Text = "No Rows Inserted into table "
End If
con.Close()
End Using
End Sub
End Class
Demo

DOTNET INTERVIEW QUESTIONS


.NET FAQS
INTERVIEW QUESTIONS IN ASP.NET,C#.NET,SQLSERVER AND .NET FRAMEWORK



Here I am posting the most common interview questions whatever everybody have faced during their interviews.

The most common question for experience persons is

Why would you like to change the company?
1) I am looking for a more challenging career in a firm with a larger employee base such as yours.
2)Chane is always welcome and new things are always there.I want to get a new opportunity and discover myself in a bigger role which would give me in extending my limitation.and also help me in building my career development.
2) Keeping in mind my career goals, the time has come for me to move onto the next step of
the ladder and make a mark for myself. This can be achieved in a company like this.
3) I am looking for the experience that can prepare me better for my future also your company is known for developing  people professionally i wanted to grow professionally as
well as financially.

What is Software Architecture?


         Partitioning the problem and the system to be built into discrete pieces
         Techniques used to create interfaces between these pieces
         Techniques used to manage overall structure and flow
         Techniques used to interface the system to its environment
         Appropriate use of development and delivery approaches, techniques and tools.