27 October 2011

Basic Connectivity C# with Sql Server

Basic Connectivity C# with Sql Server 
 
private static void CreateConnection(string queryString,string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}
 
sample connection string for C#
"Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;
server=(local)"

19 October 2011

Hash table in C#

Hash table in C#

// statements_foreach_hashtable.cs
// Using the Hashtable collection class
using System;
using System.Collections;
public class MainClass 
{
   public static void Main(string [] args) 
   {
      // Declare a Hashtable object:
      Hashtable ziphash = new Hashtable();

      // Add entries using the Add() method:
      ziphash.Add("98008", "Bellevue");
      ziphash.Add("98052", "Redmond");
      ziphash.Add("98201", "Everett");
      ziphash.Add("98101", "Seattle");
      ziphash.Add("98371", "Puyallup");

      // Display contents:
      Console.WriteLine("Zip code       City");
      foreach (string zip in ziphash.Keys) 
      {
         Console.WriteLine(zip + "          " + ziphash[zip]);
      }
   }
}

Output

Zip code       City
98201          Everett
98052          Redmond
98101          Seattle
98008          Bellevue
98371          Puyallup


Using Pointer to swap the integer using C#

Using Pointer to swap the integer using C#


/* Exchange the values by pointers. */
void swap(int *i, int *j)
{
  int temp;

  temp = *i;
  *i = *j;
  *j = temp;
}

Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET


Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET


private void btnGetAuthors_Click(object sender, System.EventArgs e)
 {
  //Create a connection to the SQL Server; modify the connection string for your environment.
  //SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Trusted_Connection=yes");
  SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;UID=myUser;PWD=myPassword;");

  //Create a DataAdapter, and then provide the name of the stored procedure.
  SqlDataAdapter MyDataAdapter = new SqlDataAdapter("GetAuthorsByLastName", MyConnection);

  //Set the command type as StoredProcedure.
  MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

  //Create and add a parameter to Parameters collection for the stored procedure.
  MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@au_lname", SqlDbType.VarChar, 40));

  //Assign the search value to the parameter.
  MyDataAdapter.SelectCommand.Parameters["@au_lname"].Value = (txtLastName.Text).Trim();

  //Create and add an output parameter to the Parameters collection. 
  MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@RowCount", SqlDbType.Int, 4));

  //Set the direction for the parameter. This parameter returns the Rows that are returned.
  MyDataAdapter.SelectCommand.Parameters["@RowCount"].Direction = ParameterDirection.Output;

  //Create a new DataSet to hold the records.
  DataSet DS = new DataSet();
  
  //Fill the DataSet with the rows that are returned.
  MyDataAdapter.Fill(DS, "AuthorsByLastName");

  //Get the number of rows returned, and assign it to the Label control.
  //lblRowCount.Text = DS.Tables(0).Rows.Count().ToString() & " Rows Found!"
  lblRowCount.Text = MyDataAdapter.SelectCommand.Parameters[1].Value + " Rows Found!";

  //Set the data source for the DataGrid as the DataSet that holds the rows.
  GrdAuthors.DataSource = DS.Tables["AuthorsByLastName"].DefaultView;

  //NOTE: If you do not call this method, the DataGrid is not displayed!
  GrdAuthors.DataBind();

  MyDataAdapter.Dispose(); //Dispose the DataAdapter.
  MyConnection.Close(); //Close the connection.
 }
     

12 October 2011

Get Multiple value from CheckedListbox in C#



Get Multiple value from CheckedListbox in C#

For Example:
 private void LoadSectionDetails()
        {
            try
            {
                DataTable dt;
                dt = objSectionDetailsBR.DisplayAll();

                if (dt.Rows.Count > 0)
                {
                    chbLSection.DataSource = dt;
                    chbLSection.DisplayMember = "SectionName";
                    chbLSection.ValueMember = "SectionID";
                }
           
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


//To get the value member and display member in Checkedlistbox in C#


 foreach (var item in chbLSection.CheckedItems)
            {
                var row = (item as DataRowView).Row;
                int id = row.Field("SectionID");
                string name = row.Field("SectionName");
              //  MessageBox.Show(id + ": " + name);
            }

List in C#

List in C#


Namespace used:System.Collections.Generic
The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.


List<string> dinosaurs = new List<string>();
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");
foreach(string dinosaur in dinosaurs)
{
      Console.WriteLine(dinosaur);
}

 dinosaurs.Insert(2, "Compsognathus");//To Insert in List

 dinosaurs.Clear(); //To Clear


Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...