Datagridview With CheckBox

September 11, 2009

/* To Create a DatagridView with checkbox */

<asp:TemplateField HeaderText=”Select”>

<HeaderTemplate>

<asp:CheckBox runat=”server” Text=”All”">All(this);” />

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBoxchkSelect” runat=”server” />

</ItemTemplate>

</asp:TemplateField>

/* Javascript function  to select all check boxes */

<script language=”javascript” type=”text/javascript”>

function All(CheckBox)

{

TotalChkBx = parseInt(‘<%= this.GridView1.Rows.Count %>’);

var TargetBaseControl = document.getElementById(‘<%= this.GridView2.ClientID %>’);

var TargetChildControl = “chkSelect“;

var Inputs = TargetBaseControl.getElementsByTagName(“input”);

for(var iCount = 0; iCount < Inputs.length; ++iCount)

{

if(Inputs[iCount].type == ‘checkbox’ && Inputs[iCount].id.indexOf(TargetChildControl,0) >= 0)

Inputs[iCount].checked = CheckBox.checked;

}

}

</script>

In code Behind

/* To get which are the values are selected in checkbox */

protected void btnsub_Click(object sender, EventArgs e)

{

CheckBox cbk = new CheckBox();

GridViewRowCollection gv = GridView1.Rows;

//foreach (GridViewRowCollection gv in GridView1.Rows)

foreach (GridViewRow gr in gv)

{

cbk = (CheckBox)(gr.Cells[0].FindControl(“chkSelect”));

if (cbk.Checked == true)

{

//  Which is the value required from the Datagrid View gr.Cells[2].Text.Trim();

Response.Write(gr.Cells[2].Text);

System.Threading.Thread.Sleep(100);

}

}

}


Javascript Adding in Codeside in Asp.net To Check Date Ranges

August 28, 2009

this.btnLoad.Attributes.Add(“onclick”,”javascript:return CheckDate();”);

<script language=”javascript” type=”text/javascript”>
function CheckDate()
{
var date1=document.getElementById(‘txtFromDate’).value;
var date2=document.getElementById(‘txtToDate’).value;

var datDate1=new Date();
var datDate2=new Date();

datDate1= Date.parse(date1);
datDate2= Date.parse(date2);
datediff = ((datDate2-datDate1)/(24*60*60*1000))
//To Check whether Days are greater than 7 days or not
if(datediff>7)
{
alert(‘You can not select more than 7 days’);
return false;
}
else
{
return true;
}

if(date1 > date2)
{
alert(‘please select proper dates’);
return false;
}
else
{
return true;
}
}
</script>


To add Serial Number in DataGrid

August 28, 2009

<asp:templatecolumn headertext=”S.No” >
<itemtemplate>
<%# Container.ItemIndex+1 %>
</itemtemplate>
</asp:TemplateColumn>


Transactions in Sql Server

August 28, 2009

To Check any active transactions which is not commited:

SELECT * FROM sys.dm_tran_session_transactions

/* This work like activity Monitor */

To See all Executing Queries:

sp_who2 active

DBCC inputbuffer(blkby) [To check sp_who2 blkby id executing query or procedure]

declare @table table(spid int,status varchar(50),[login] varchar(100),hostname varchar(100),
blkby varchar(100),dbname varchar(15),command varchar(100),cputime int,diskio int,
lastbatch varchar(100),programname varchar(100),spid1 int,requestid int)
insert into @table
exec sp_who2 active
select spid,status,login,hostname,cputime,command,blkby from @table where ltrim(rtrim(hostname))<>’.’ and dbname<>’master’
and ltrim(rtrim(blkby))<>’.’ and status<>’sleeping’ and isnull(login,”)<>”
order by hostname

DBCC inputbuffer(blkby)


How to get last date of a Month in Sql Server

April 15, 2009

declare @date varchar(10)

set @date=convert(varchar,year(getdate()))+’-'+convert(varchar,(month(getdate())+1))+’-01′

select dateadd(day,-1,@date)

Logic is which month we want to find the last date take that month.

Suppose i want to find last day of   14-04-2009

Add 1 month to this and create a format of next month first day

the next month will be 05 ,2009

2009-05-01

With dateadd function add -1 day to that function . So it will come to the previous month last day.

dateadd(day,-1,’2009-05-01′)

it will display   ‘2009-04-31


Inserting using one insert statement

March 27, 2009

USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT ‘First’ ,1
UNION ALL
SELECT ‘Second’ ,2
UNION ALL
SELECT ‘Third’ ,3
UNION ALL
SELECT ‘Fourth’ ,4
UNION ALL
SELECT ‘Fifth’ ,5
GO


creating tables from other tables in sql 2005

March 27, 2009

Method 1 : INSERT INTO SELECT
This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are are not required to list them. I always list them for readability and scalability purpose.
USE AdventureWorks
GO
—-Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
—-INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Contact
WHERE EmailPromotion = 2
—-Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
—-Clean Up Database
DROP TABLE TestTable
GO

Method 2 : SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
USE AdventureWorks
GO
—-Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
—-Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
—-Clean Up Database
DROP TABLE TestTable
GO


Sending Request And Get Response with POST Method

February 3, 2009
Imports System.IO  
Imports System.Net  
Partial Class Default1  
    Inherits System.Web.UI.Page  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        Dim strURL As String = “”  
        Dim strPostData As String = “”  
        Dim strResult As String = “”  
        Dim wbrq As HttpWebRequest  
        Dim wbrs As HttpWebResponse  
        Dim sw As StreamWriter  
        Dim sr As StreamReader  
  
        ’ Set the URL to post to  
        strURL = “http://www.webcom.com/cgi-bin/form”  
        ’ Post some values to the page  
        strPostData = String.Format(“your_name={0}&userid={1}&form_name={2}”, “Mark Smith”, “webcom”, “tutortest”)  
  
        ’ Create the web request  
        wbrq = WebRequest.Create(strURL)  
        wbrq.Method = “POST”  
        ’ We don’t always need to set the Referer but in this case   
        ’ the page we are posting to will only issue a response if we do  
        wbrq.Referer = “http://www.webcom.com/cgi-bin/form”  
        wbrq.ContentLength = strPostData.Length  
        wbrq.ContentType = “application/x-www-form-urlencoded”  
  
        ’ Post the data  
        sw = New StreamWriter(wbrq.GetRequestStream)  
        sw.Write(strPostData)  
        sw.Close()  
  
        ’ Read the returned data   
        wbrs = wbrq.GetResponse  
        sr = New StreamReader(wbrs.GetResponseStream)  
        strResult = sr.ReadToEnd.Trim  
        sr.Close()  
  
        ’ Write the returned data out to the page  
        TextBox1.Text = strResult  
    End Sub  
End Class  

To get one form values in another Form

May 22, 2008

Write this code in second Form

label1.Text = Application.OpenForms["FirstForm"].Controls["controlname"].Text;


Some SQL Tips

May 22, 2008

How to replace spaces in text stored in sql server with in function or Store Proceduer?

declare @name as varchar(50)
declare @rep as varchar
set @rep=’ ‘
set @name=’kamal   raja       v’
select replace(@name,@rep,”)

It will replaces the characters what ever you define in @rep that character is replaced from @name.

It is help full when writing functions or stored procedures , bcoz sql does not provides directly Trim() function which replaces spaces between given text.

select getdate()                      —– > To get Current Date

select datepart(day,getdate())  —–> To get Day

select datepart(month,getdate()) —-> To get Current Month

select datepart(year,getdate()) —-> To get Current Year

Some examples of converting date format:

select convert(varchar,getdate(),111)  =====>  ‘2008/5/21′

select convert(varchar,getdate(),106)  =====> ‘21 May 2008′