Monday 30 July 2012

asp.net application state example: how to use application state in asp.net

asp.net application state example: how to use application state in asp.net


  1. <%@ Page Language="C#" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e) {  
  7.         int counter = 0;  
  8.         if(Application["ButtonClickCounter"] !=null)  
  9.         {  
  10.             counter = (int)Application["ButtonClickCounter"];  
  11.         }  
  12.         counter++;  
  13.         Application["ButtonClickCounter"] = counter;  
  14.         Label1.Text = "Button Clicked: " + counter.ToString() + " times";  
  15.     }  
  16. </script>  
  17.   
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head runat="server">  
  20.     <title>asp.net application state example: how to use application state in asp.net</title>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <h2 style="color:Teal">asp.net application state example</h2>  
  26.         <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Crimson">  
  27.         </asp:Label>  
  28.         <br /><br />  
  29.         <asp:Button   
  30.             ID="Button1"   
  31.             runat="server"   
  32.             Text="Show Button Click Status"  
  33.             OnClick="Button1_Click"  
  34.             Font-Bold="true"  
  35.             ForeColor="SeaGreen"  
  36.             />  
  37.     </div>  
  38.     </form>  
  39. </body>  
  40. </html>  

asp.net application state example: how to lock and unlock application state in asp.net


asp.net application state example: how to lock and unlock application state in asp.net


  1. <%@ Page Language="C#" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e) {  
  7.         Application.Lock();  
  8.           
  9.         int clickCounter = 0;  
  10.         if(Application["ClickCounter"] !=null)  
  11.         {  
  12.             clickCounter = (int)Application["ClickCounter"];  
  13.         }  
  14.         clickCounter++;  
  15.         Application["ClickCounter"] = clickCounter;  
  16.         Application.UnLock();  
  17.   
  18.         Label1.Text = "Button Clicked: " + clickCounter.ToString() + " times";  
  19.     }  
  20. </script>  
  21.   
  22. <html xmlns="http://www.w3.org/1999/xhtml">  
  23. <head id="Head1" runat="server">  
  24.     <title>asp.net application state example: how to lock and unlock application state in asp.net</title>  
  25. </head>  
  26. <body>  
  27.     <form id="form1" runat="server">  
  28.     <div>  
  29.         <h2 style="color:Red">asp.net application state example: Lock and UnLock</h2>  
  30.         <asp:Label   
  31.             ID="Label1"   
  32.             runat="server"   
  33.             Font-Size="Large"   
  34.             ForeColor="DodgerBlue"  
  35.             >  
  36.         </asp:Label>  
  37.         <br /><br />  
  38.         <asp:Button   
  39.             ID="Button1"   
  40.             runat="server"   
  41.             Text="Show Button Click Status"  
  42.             OnClick="Button1_Click"  
  43.             Font-Bold="true"  
  44.             ForeColor="DodgerBlue"  
  45.             />  
  46.     </div>  
  47.     </form>  
  48. </body>  
  49. </html>  

Search Records in DataSet .


Search Records in DataSet .



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

class Check
{
SqlConnection con;
SqlDataAdapter adap;
DataSet ds;

public Check()
{
con=new SqlConnection("initial Catalog=PraticeCAC;data Source=OM-PC;integrated security=yes");
ds=new DataSet();
}
public void show_Recordcount()
{
adap=new SqlDataAdapter("Select *from tbl_studentRecords",con);
adap.Fill(ds,"student");
int t=ds.Tables["student"].Rows.Count;// this code for count the row or records in table
for(int i=0;i<t; i++)// this code for show data records in table .
{
Console.WriteLine(ds.Tables["student"].Rows[i]["rollno"]+""+ds.Tables["student"].Rows[i]["stud_name"]);
}
Console.WriteLine(t);
}

public static void Main()
{
Check c=new Check();
c.show_Recordcount();
}
}

Friday 20 July 2012

How to return more than one value from a method/function in c#? ans-using out keyword.



How to return more than one value from a method/function in c#?        ans-using out keyword.




using System;
class test
{
public void caluculate(int x,int y, out int sum,out int mul)
{
sum=x+y;
mul=x*y;
}


public static void Main()
{
test t=new test();
int j,k;
t.caluculate(10,20,out j,out k);
Console.WriteLine(j);
Console.WriteLine(k);
Console.ReadLine();
}
}

Saturday 14 July 2012

Create procedure with output parameters.




Create procedure with output parameters.

create proc searchrecordemp
@EmpId int,
@EmpName varchar(200) output,
@EmpSalary Decimal(10,2) output
as
begin
select @EmpName=EmpName,@EmpSalary=EmpSalary from tbl_employee
where EmpId=@EmpId
end
---------------------------------------------------------------------------------
declare @empname varchar(200)
declare @empsalary decimal(10,2)
exec searchrecordemp 1, @empname output,@empsalary output
select @EmpName as EMPNAME,@EmpSalary as EMPSALARY

Calling procedure with output parameters from front end using c#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlParameter pr, pr1;
   public _Default()
   {       con = new SqlConnection("initial catalog=PraticeCAC; Data Source=OM-PC; integrated security=yes");
   }    
    protected void Page_Load(object sender, EventArgs e)
    {
        fill();
    }
    public void fill()
    {
        con.Open();
        cmd = new SqlCommand("searchrecordemp", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@EmpId",1);
        pr=cmd.Parameters.Add("@EmpName",SqlDbType.Char,200);
        pr.Direction = ParameterDirection.Output;
        pr1 = cmd.Parameters.Add("@EmpSalary", SqlDbType.Decimal, 10);
        pr1.Direction=ParameterDirection.Output;
        cmd.ExecuteReader();
        Response.Write(pr.Value.ToString()+" "+pr1.Value.ToString());
        con.Close();

    }
}

Using Primary Key with default.


Using Primary Key with default.........

create table demo
(EmpId int primary key,EmpSecondaryaddress varchar(200)
default 'not mention'
)

select *from demo

insert into demo (EmpId,EmpSecondaryaddress)
values (102,'noida')

These are sql query using to sear records about procedure ,function ,tables..etc without using UI.

These are sql query using to sear records about procedure ,function ,tables..etc without using UI.



exec dbo.Search_data12 1

----------to searching stored procedure without UI.-------------
-------steps-1-execute stored proc
-------------2-tools--select sqlproplier--select sp--run---erase-

-------sp_helptext Search_data12--copy table ----paste-see
sp_helptext Search_data12

CREATE proc [dbo].[Search_data12]
 @rollno int
 as
 begin
  select stud_name, std_course from tbl_studentRecords where @rollno=rollno
 end

 --------------------to see all tables in database----------
 select * from sys.tables ----where type='U'
 select * from sys.procedures -----to procedures
-----------------------------------------------------------

select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='tbl_studentRecords'-------
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='tbl_studentRecords'-------

---------------------------------------------------------------for functions--
select * from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE='function name'
select * from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE='Search_data12'

system information


If you want to system information (window or web application) than you can try this code.

This is front end coding in c#.

using System.IO;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SystemInfo();
        }
    }
    public void SystemInfo()
    {
       
        string[] s = Environment.GetLogicalDrives();
        foreach (string item in s)
        {
            DropDownList1.Items.Add(new ListItem(item));
        }
    }
   
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        DirectoryInfo d = new DirectoryInfo(DropDownList1.SelectedItem.Text);
        DirectoryInfo[] da = d.GetDirectories();
        foreach (DirectoryInfo item in da)
        {
            DropDownList2.Items.Add(new ListItem(item.Name));
        }
    }
}


for aspx page you can take two dropdownlist .