Showing posts with label C Sharp. Show all posts
Showing posts with label C Sharp. Show all posts

Friday, February 17, 2012

How to send mail using SMTP Mail in asp.net

using System;
using System.Web.Mail;

public class SmtpMailExample
{
public static void Main()
{
string FromSMTP = "from@asp.net";
string ToSMTP = "to@smtp.net";
string SubjectSMTP = "This is a SMTP Test Mail Example Message";
string BodySMTP = "Hi .";

SmtpMail.SmtpServer = "192.168.1.151";
SmtpMail.Send(FromSMTP, ToSMTP, SubjectSMTP, BodySMTP);
}
}

Saturday, May 21, 2011

using SqlDataAdapter Update table

using System;

using System.Data;

using System.Data.SqlClient;



class UpdateusingdataobjAdapter
{
static void Main()
{
string cn="server=localhost;database=DBTEST;uid=sa;pwd=sa";
SqlConnection Connection = new SqlConnection(cn);
string query = @"select * from tblemp ";
string update = @"update tblemp set name = @name where id = @id";
try
{

   SqlDataobjAdapter objAd = new SqlDataobjAdapter();

   objAd.SelectCommand = new SqlCommand(query, Connection );
   DataSet objDs = new DataSet();  

   objAd.Fill(objDs, "tblemp");
   DataTable objDt = objDs.Tables["tblemp"];

   objDt.Rows[0]["name"] = "Ravi";

   foreach (DataRow row in objDt.Rows)
   {

    Console.WriteLine(

                  "{0} {1}",

                  row["name"].ToString(),

                  row["lastname"].ToString();

            }



     // Update tblemps

    SqlCommand objcmd = new SqlCommand(update, Connection );

    objcmd.Parameters.Add("@name",SqlDbType.NVarChar,15, "name");

    SqlParameter parm = objcmd.Parameters.objAdd("@id",SqlDbType.Int,4,"id");

    parm.SourceVersion = DataRowVersion.Original;

    objAd.UpdateCommand = objcmd;

    objAd.Update(objDs, "tblemp");

    }
catch(Exception e)
 {

            Console.WriteLine("Error: " + e);

 }
finally
 {

            Connection.Close();

 }

      } 

   }

Thursday, May 19, 2011

Way of Insert Data in Database Table with ADO.Net

There are many way Insert data in database table.
I described Five way here.
1. using sql command techniques insert data into database table 
2.Using Bind parameters sql command techniques insert data into database table 
3.using sql command with Parameters techniques insert data into database table 
4.Using ExecuteScalar and SqlCommand Insert data into Database Table 
5.using CommandBuilder insert datat in form datatable in datadase 
5 is Best and Optimize way of insert data into database table because in with 
using conncetionless (SqlDataAdpter) concepts and in insert in datatable form so 
it is best in Performance view and it can use with asp.net where large amount of
users are working.  

using CommandBuilder insert datat in form datatable in datadase

This is the best and optimize way of insert data in to database table 
with ado.net
InsertingDataUsingCommandBuilder
using System;
using System.Data;
using System.Data.SqlClient;

public class InsertingDataUsingCommandBuilder
{
 
static void Main(string[] args)
 {
string Cn="DataSource=(local);InitialCatalog=CaseManager;Integrated Security=true";
string Query="SELECT ID, Contact, Email FROM Test";
SqlConnection objConnection = new SqlConnection(Cn);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(Query,objConnection);
SqlCommandBuilder objCmd = new SqlCommandBuilder(objDataAdapter);
DataSet objDataSet = new DataSet();
objDataAdapter.Fill(objDataSet);
DataRow objRow = objDataSet.Tables[0].NewRow();
objRow["ID"] = 520;
objRow["Contact"] = "Bill";
objRow["Email"] = "Ray";
objDataSet.Tables[0].Rows.Add(objRow);
objDataAdapter.Update(objDataSet);
        }
    }

Using ExecuteScalar and SqlCommand Insert data into Database Table

using System;

using System.Data;

using System.Data.SqlClient;
   

public class InsertingDataUsingSQLStatementsExecuteScalar

{

static void Main(string[] args)

{
string CN="Data Source=(local); Initial Catalog = DBNAME; Integrated Security=true; 
SqlConnection objConnection = new SqlConnection(CN);
            

String Query ="INSERT INTO tblTest(ID, Contact, Email) VALUES(3, 'Bill', 'John')";

SqlCommand objCmd = new SqlCommand(Query, onConnection);objConnection.Open();
objCmd.ExecuteScalar();
objConnection.Close();

} 
}


using sql command with Parameters techniques insert data into database table

 
 
 
using System;
using System.Data;
using System.Data.SqlClient;

class UsingParameterAndCommand
 {
    public static void Main() 
{
string cn="server=localhost;database=DBTEST;uid=sa;pwd=sa";
 SqlConnection Connection = new SqlConnection(cn);
        
        SqlCommand objCmd = Connection.CreateCommand();
        objCmd.CommandText =
          "INSERT INTO ASPTable (" +
          "  CustomerID, CompanyName, ContactName" +
          ") VALUES (" +
          "  @CustomerID, @CompanyName, @ContactName" +
          ")";
        objCmd.Parameters.Add("@CustomerID", "AB001");
        objCmd.Parameters.Add("@CompanyName", "MicroSoft");
        objCmd.Parameters.Add("@ContactName", "Bill");
        Connection.Open();
        objCmd.ExecuteNonQuery();
        Console.WriteLine("Successfully added row to ASPTable ");

        Connection.Close();
    }
}

Using Bind parameters sql command techniques insert data into database table

This techniques is better than
using sql command techniques insert data into database table 
because SqlDataAdapter is used here but not optimize solution. 
Bind Parameters techniques for Insert data
using System;
using System.Data;
using System.Data.SqlClient;

class bindParametersExample
{
      static void Main() 
{
string ConnectionString ="server=(local)\\SQLEXPRESS;database=DBTEST;
Integrated Security=SSPI";
string Query = @"select * from aspdotnetTable";
string InsertQuery = @"insert into aspdotnetTable(firstname,lastname)
values(@firstname,@lastname)";

         SqlConnection objConnection = new SqlConnection(ConnectionString );

         try{
            SqlDataAdapter objAd = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand(Query, objConnection );

            DataSet objDs = new DataSet();   
            objAd.Fill(objDs , "aspdotnetTable");

            DataTable objDataTable = objDs.Tables["aspdotnetTable"];

            DataRow objRow = objDataTable.NewRow();
            objRow["firstname"] = "Bill";
            objRow["lastname"] = "Gates";
            objDataTable.Rows.Add(objRow);

            foreach (DataRow row in objDataTable.Rows){
               Console.WriteLine(
                  "{0} {1}",
                  row["firstname"].ToString().PadRight(15),
                  row["lastname"].ToString().PadLeft(25));
            }

            // Insert data in to aspdotnetTable
       SqlCommand objCmd = new SqlCommand(InsertQuery, objConnection);
       objCmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 10, "firstname");
       objCmd.Parameters.Add("@lastname",  SqlDbType.NVarChar, 20, "lastname");
       objAd.InsertCommand = objCmd;
       objAd.Update(objDs, "aspdotnetTable");
         } catch(Exception e) {
            Console.WriteLine("Error: " + e);
         } finally {
            objConnection.Close();
         }
      }  
   }

using sql command techniques insert data into database table

we must add using System.Data.SqlClient; namespace. 
now following code you can use.
This is Vary old Techniques. If we use techniques with Asp.net 
there is Pooling required because if there is large amount of users 
use this page than it is difficult to handle connnection.  
Insert into database using sqlcommand 
using System;
using System.Data;
using System.Data.SqlClient;

   class sqlcommandexample
{
      static void Main() 
      {
string CN="server=(local)\\SQLEXPRESS;database=DBTEST;Integrated Security=SSPI"; 
SqlConnection objCn = new SqlConnection(CN);
         SqlCommand objCmd= objCn.CreateCommand();

         try 
         {
objCn.Open();

objCmd.CommandText = "CREATE DATABASE NewDB";
Console.WriteLine(objCmd.CommandText);

objCmd.ExecuteNonQuery();
Console.WriteLine("Now is Database created and database");
objCn.ChangeDatabase("NewDB");

objCmd.CommandText = "CREATE TABLE aspdotnetTable(COL1 integer)";
Console.WriteLine(objCmd.CommandText);
Console.WriteLine("NO Rows Affected is: {0}", objCmd.ExecuteNonQuery());

objCmd.CommandText = "INSERT INTO aspdotnetTable VALUES (100)";
Console.WriteLine(objCmd.CommandText);
Console.WriteLine("NO Rows Affected is: {0}", objCmd.ExecuteNonQuery());
         
         } catch (SqlException ex) {
         
            Console.WriteLine(ex.ToString());
         
         } finally {  
         
            objCn.Close();
            Console.WriteLine("Connection Closed.");
         
         }
      }
   }

Wednesday, April 27, 2011

Property in Tutorial in C sharp

Property is Smart Method.
below given property example.

Tuesday, April 26, 2011

Indexer in Csharp Example

The Indexer is also known as Smart Array.
Benefit with Indexer is that we not need to define Limit Value like Array.
Example Of Indexer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexer
{
class Tutorial
{
string [] name=new string[15];
public string this[int range]
{
set { name[range] = value; }
get { return name[range]; }
}
static void Main(string[] args)
{
Tutorial p = new Tutorial();
p[0] = "Mike";
p[1] = "Milan";
p[2] = "Vihar";
p[3] = "Vivek";
p[4] = "Sharad";
Console.Write(" {0} \n {1} \n {2} \n {3} \n {4}",p[0],p[1],p[2],p[3],p[4]);


Console.ReadLine();

}
}

Monday, April 25, 2011

Exception handling Divide By Zero Exception

DivideByZeroException Exception Handling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exception handling 
{
    class Program
    {
        
        
        static void Main(string[] args)
        {
            int i;
            int j;
            int k;
            Console.WriteLine("Enter 1st No: ");
            i = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter 2nd No: ");
            j = int.Parse(Console.ReadLine());
            try
            {
                if (j == 0)
                {
                    throw new DivideByZeroException();
                }
                k = i / j;
                Console.WriteLine("Ans is " + k);
            }
            catch (DivideByZeroException)
            {
                    Console.WriteLine("Divide By Zero Not Possible..");
            
            }

            finally
            {
                Console.ReadLine();
            }
        }
    }
}
 

Saturday, April 23, 2011

Exception Handling IndexOutOfRangeException

Exception handling for index outof range exception in c sharp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ass_45
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] name = new int[3];
            Console.WriteLine("Enter Array Value:");
            try
            {
                for (int i = 0; i <= 3; i++)
                {

                    name[i] = Convert.ToInt32(Console.ReadLine());

                }
                for (int i = 0; i <= 3; i++)
                {
                    Console.WriteLine("Array" + name[i]);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Array Out of Index Ha Ha Ha");
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

Generics example in c sharp

Generics is belongs from System.Collections.Generic class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generics
{
    public class Test
    {
        public T EmpID
        {
            set;
            get;
        }

        public void show(T t,N n)
        {
            if (typeof(T) == typeof(int) && typeof(N) == typeof(int))
            {
                int x = Convert.ToInt32(t);
                int y = Convert.ToInt32(n);
                Console.WriteLine(x + y);
            }
            else if (typeof(T) == typeof(string) && typeof(N) == typeof(int))
            {
                Console.WriteLine("The " + t + "'s Age is " + n);
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test ob = new Test();
            ob.EmpID = 10;
            ob.show(10, 20);

            Test ob1 = new Test();                        
            ob1.show("Rajesh",20);
        }
    }
}

Saturday, December 18, 2010

Why in C Sharp or java not having Pointer ?

Because in C Sharp CLR is Work Memory management.It will change location in milliseconds or Address is not Fixed.that's way not having pointer.This same thing in Java. Same work do in java By JVM.