DotNet

For Authentication – Put in Master
  
myCon.Open();
cmd = new SqlCommand("select rol,use_id,pas_wor from login_tab where use_id='" + txtuname.Text + "' and pas_wor='" + txtpwd.Text + "'", myCon);
  SqlDataReader dr = cmd.ExecuteReader();
  while (dr.Read())
  {
  Session["uname"] = dr.GetValue(1).ToString();
  Session["role"] = dr.GetValue(0).ToString();
  }
  myCon.Close();
  try
  {
  string role = Session["role"].ToString();
  switch (role)
  {
  case "~u":
  cmd = new SqlCommand("select stu_nam from student_master where stu_reg='" + Session["uname"] + "'", myCon);
  fordr();
  Response.Redirect("students.aspx");
  break;
  case "~s":
  cmd = new SqlCommand("select sta_nam from staff_master where sta_id='" + Session["uname"] + "'", myCon);
  fordr();
  Response.Redirect("staffs.aspx");
  break;
  case "~a":
  Session["user"] = "Admin";
  Response.Redirect("admin.aspx");
  break;
  
  }
  
  }
  catch
  {
  }
  
For Authentication – Put in .cs
  
if (!IsPostBack)
  {
  try
  {
  lbluser.Text = "Welcome " + Session["user"].ToString() + ",";
  
  if (Session["role"].ToString() != "~a")
  Response.Redirect("index.aspx");
  }
  catch
  {
  Response.Redirect("index.aspx");
  }  
  } 

For Save Image in Database & Hard Drive
  
// This Code Use to Create Folder
  // using system.IO;
  // Directory.CreateDirectory(MapPath("App_Themes\\Images\\")+"\\"+FolderName.Text);
  if (FileUpload1.HasFile)
  {
  string img;
  img = Convert.ToString("images/" + FileUpload1.FileName);
  FileUpload1.SaveAs(Server.MapPath("\\AJ-Shoppingcart\\images\\" + FileUpload1.FileName);
  SqlCommand cmd = new SqlCommand("insert into categ values('" + txtCon1.Text + "','" + txtCon2.Text + "','" + img + "','" + txtCon3.Text + "','" + DropDownList1.Text + "')", con);
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
  lblmsg.Text = "Record Inserted Succesfully";
  lblmsg.ForeColor = Color.Black;
  }
  else
  {
  lblmsg.Text = "Please Upload Image";
  }


For Find Value From DataList when Click the Item
  
This Code will fire when you click the item (use postback controls like button,image)
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
  {
  int a;
  a=(e.Item.ItemIndex);
  Label lbl1 = (Label)DataList1.Items[a].FindControl("cnameLabel");
  Label lbl2 = (Label)DataList1.Items[a].FindControl("cidLabel");
  Label lbl3 = (Label)DataList1.Items[a].FindControl("costLabel");
  Label lbl4 = (Label)DataList1.Items[a].FindControl("ima");  
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
   
}
  
For Use Stored Procedure in .cs
  
  SqlCommand cmd = new SqlCommand("spchpwd", myCon);
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.Add("use_id", SqlDbType.VarChar).Value = txtuid.Text;
  cmd.Parameters.Add("old_pwd", SqlDbType.VarChar).Value = txtopwd.Text;
  cmd.Parameters.Add("pas_wor", SqlDbType.VarChar).Value = txtnpwd.Text;
  cmd.Parameters.Add("sec_que", SqlDbType.VarChar).Value = txtsecque.Text;
  cmd.Parameters.Add("sec_ans", SqlDbType.VarChar).Value = txtans.Text;
  myCon.Open();
  cmd.ExecuteNonQuery();
  myCon.Close();
  

For Display Particular Column in Colors 
  
Note: Write this code only in RowDataBound for Page Settings
   
  protected void gvattenview_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (IsPostBack)
  {
  
  ListItemType lt = (ListItemType)e.Row.RowType;
  if (lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
  {
   
  DataRowView drv = (DataRowView)e.Row.DataItem;
  
  if (drv[5].ToString() == "Absent")
  {
  e.Row.Cells[1].ForeColor = Color.Maroon;

  }
  }
  
  }
}
  
  
    
  
For Display Particular Row in Colors
  
Note: Write this code only in RowDataBound for Page Settings
  
protected void ttgrid_RowCreated(object sender, GridViewRowEventArgs e)
  {
  ListItemType lt =(ListItemType)e.Row.RowType;
  if (lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
  {
  DataRowView drv = (DataRowView)e.Row.DataItem;
  string day=DateTime.Now.DayOfWeek.ToString();
  if (drv[0].ToString() == day)
  {
  //e.Row.BackColor = Color.YellowGreen;
  for(int i=1;i<=8;i++)
  {
  e.Row.Cells[i].BackColor = Color.YellowGreen;
  e.Row.Cells[i].ForeColor = Color.Black;
  }
  }
  }
  }

For Generate ID Automatically
  
Select isnull(max(convert(numeric,substring(sta_id,4,10))),0)+1 from staff_master
  
For Get the Connection String Globally 
  
Note: Write this code in web.config
  

 

  
For Use the Connection
  
string con = ConfigurationManager.ConnectionStrings["osmCon"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(con);
  
For Split the Date
  
  string[] a = new string[3]; 
  string dm;
  int d,m,y;
  dm = Convert.ToString(Session["edate"]);
  a = dm.Split('/');
  y = Convert.ToInt32(a[2]);
  d = Convert.ToInt32(a[1]);
  m = Convert.ToInt32(a[0]);
  
For Timer Calculation
  
Note: Time Set in Database. Format: [m:h]

protected void Timer1_Tick(object sender, EventArgs e)
  {  
  str1 = Label1.Text; à[00:00:00]
  timeArray = str1.Split(':');
  s = Convert.ToInt32(timeArray[2]);
  m = Convert.ToInt32(timeArray[1]);
  h = Convert.ToInt32(timeArray[0]);
  s = s + 1;
  if ((m == min)&&(h==hou))
  {
  butclick(); à Not Use for us
  }
  else 
  {
  if(s!=60)
  {
  if(s<=9)
  {
  Label1.Text = "0"+Convert.ToString(h) + ":0" + Convert.ToString(m) + ":0" + Convert.ToString(s);
  }
  else
  {
  Label1.Text = "0"+Convert.ToString(h) + ":0" + Convert.ToString(m) + ":" + Convert.ToString(s);
  }
  }  
  else if((s==60)&&(m!=60))
  {
  if (m <= 9)
  {
  s = 0;
  m = m + 1;
  Label1.Text = "0"+Convert.ToString(h) + ":0" + Convert.ToString(m) + ":0" + Convert.ToString(s);
  }
  else
  {
  s = 0;
  m = m + 1;
  Label1.Text = "0"+Convert.ToString(h) + ":0" + Convert.ToString(m) + ":0" + Convert.ToString(s);
  }
  }
  else if (m == 60)
  {
  s = 0;
  m = 0;
  h = h + 1;
  Label1.Text = "0"+Convert.ToString(h) + ":0" + Convert.ToString(m) + ":0" + Convert.ToString(s);
  }
  }
  }
  
  
For Insert
  
Note: If Product already Exists in db it will update record with old Qty otherwise it will insert new record. [ cname means Category Name ]
  
Query:
  
IF EXISTS 
(select * from cart where pid=@pid and cname=@cname and cid=@cid)
UPDATE cart set qty=qty+1 where cname=@cname and pid=@pid and cid=@cid
ELSE
Insert into cart(pname,pid,cost,qty,ima,cname,dop,cid) values(@pname,@pid,@cost,1,@ima,@cname,Getdate(),@cid)
  

News Scroller
  
strScrolling = "";
  while (sqlRdr.Read())
  {
  string str = sqlRdr.GetValue(0).ToString();
  strScrolling = strScrolling + "" + sqlRdr.GetValue(0) + "  
" + "" + "Date: " + sqlRdr.GetValue(1).ToString() + "
" + "" + sqlRdr.GetValue(3).ToString() + "

";
  }
  strScrolling = strScrolling + "
";
  sqlRdr.Close();
  Literal1.Text = strScrolling;
  
  
News Scroller with Page Link
  
while(sqlRdr.Read())
  {
  strScrolling = strScrolling + "('newsDetail.aspx?NewsId="+sqlRdr.GetValue(0)+"','NewsDetail','width=400,height=400;toolbar=no;');"+">"+ sqlRdr.GetValue(1) +"

";
  }
  
  
Reverse the String
  
  char [] strArray = TextBox1.Text.ToCharArray(); 
  Array.Reverse( strArray ); 
  string strReversed = new string( strArray ); 
  Label1.Text = strReversed;
  
Difference between Date
  
  string dt1 = DateTime.Parse("08/06/2007").ToString("dd/MM/yyyy");
  string dt2 = DateTime.Parse("10/06/2007").ToString("dd/MM/yyyy");
  DateTime dt4 = DateTime.Parse(dt1);
  DateTime dt5 = DateTime.Parse(dt2);
  TimeSpan tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
  //value from greater date
  Response.Write("Difference between " + dt4.ToString() + " and " +
  dt5.ToString() + " is " + tsDiff.Days.ToString() + " days." + "
");
  //Output: Difference between 08-06-2007 00:00:00 and 10-06-2007 00:00:00 is 2 days.

Difference between Time
  
  string dt1 = DateTime.Parse("04:30:00").ToString("HH:mm:ss");
  string dt2 = DateTime.Parse("19:30:00").ToString("HH:mm:ss");
  DateTime dt4 = DateTime.Parse(dt1);
  DateTime dt5 = DateTime.Parse(dt2);
  TimeSpan tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
  //value from greater date
  Response.Write("Difference between " + dt4.ToString() + " and " + dt5.ToString() + " 
  is " + tsDiff.Hours.ToString() + " Hours." + tsDiff.Minutes.ToString()+"
");
  //Output: Difference between 22-06-2007 04:30:00 and 22-06-2007 19:30:00 is 15 Hours.
  
  
For Find Weekend
  
  string dt1 = DateTime.Parse("03/10/2008").ToString("dd/MM/yyyy");
  DateTime dt4 = DateTime.Parse(dt1);
  DayOfWeek dow = dt4.DayOfWeek;
  if ((dow == DayOfWeek.Sunday) == (dow == DayOfWeek.Saturday))
  {
  Response.Write("Hurray its weekend");

 
  "+sqlRdr.GetValue(2).ToString()+"