Tuesday, 21 April 2015

Difference between tostring() and convert.tostring()

Standard
The basic difference them is Convert.toString(Variable) handles null values even if variable value become null but variable.toString() will not handle null values it will throw a NULL reference exception error.

Convert is always safe as compair to toString().


example :


using System;
using System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
using
 System.Threading.Tasks; 
namespace csharpExamples
{
   
class Program
   {

      
static void Main(string[] args)
      {

          
string abc = "" ;
          abc = 
null;
          
Console.Write(abc.ToString());
          
Console.ReadKey();
      }

 }


And if you use Convert.ToString() . then you need to change this line of code ..

Console.Write(abc.ToString());

          to 
Console.Write(Convert.ToString(abc));

Differeces Between ExecuteReader,ExecuteScalar,ExecuteNonQuery

Standard
ExecuteReader

ExecuteReader will be used to return the set of Rows,on execution of Sql query or Stored Procedure using command object.This  one is the forward only retrieval of records and it is used to read the table value from first to last.

ExecuteScalar

Execute Scalar will return single row single column value ( i.e single value ) on execution of Sql query or Stored Procedure using command object.It is very fast to retrieve single value form database.

example :

protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=ss;Integrated Security=true;Initial Catalog=MyDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Student_Name,Roll from student", con);
string result = (string)cmd.ExecuteScalar();
if (!string.IsNullOrEmpty(result))
{
lblDetails.Text = result;
}
else
{
lblDetails.Text =  "No value Selected" ;
}
con.Close();
}

}


ExecuteNonQuery

ExecuteNonQuery will return number of rows effected with Insert,Update,Delete operations.This method used only for Insert,Update,Delete,Create and Set Statement.

example

protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection con=new SqlConnection("Data Source=ss;Integrated Security=true;Initial Catalog=MyDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into student(Student_Name,Roll) values(@Name,@Roll)", con);
cmd.Parameters.AddWithValue("@Name", "Kapil");
cmd.Parameters.AddWithValue("@Roll", "12");
int count= cmd.ExecuteNonQuery();
if(count>=1)
{
lblDetails.Text =  count.ToString();
}
else
{
lblDetails.Text = "0" ;
}
con.Close();
}
}

Difference Between DataReader,DataSet,DataAdapter and DataTable in C#.

Standard
DataReader :

DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader is used to iterate through Resultset that came from server and it will read one record at a time because of the memory consumption will be less and it will fetch tha very fast when compared with Dataset.

example :

Protected void BindGridview()
{
using (SqlConnection conn = new SqlConnection("Data Source=ss;Integrated Security=true;Initial Catalog=MyDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Student_name,Roll,from student", conn);
SqlDataReader sdr = cmd.ExecuteReader();
gvUserInfo.DataSource = sdr;
gvUserInfo.DataBind();
conn.Close();
}
}


DataSet :

DataSet is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of DataTables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also DataSet provides you with rich features like saving data as XML and loading XML data.

example :

protected void BindGridview()
{
    SqlConnection conn = new SqlConnection("Data Source=ss;Integrated Security=true;Initial Catalog=MyDB");
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select Student_Name,Roll from student", conn);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gvUserInfo.DataSource = ds;
    gvUserInfo.DataBind();
}


DataAdapter :

DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture. 

example :

protected void BindGridview()
{
    SqlConnection con = new SqlConnection("Data Source=ss;Integrated Security=true;Initial Catalog=MyDB");
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select Student_Name,Roll from student", conn);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gvUserInfo.DataSource = ds;
    gvUserInfo.DataBind();
}