Wednesday, 6 July 2011

how to call confirmation box on aspx.cs file and clicking ok button on confirmation box it will redirected another page


For displaying confirmation box
write code on aspx.cs file where u want to call confirmation box
ClientScript.RegisterStartupScript(GetType(), "jsScript", "Setconfirm();", true);

write Setconfirm() function on aspx file under <script language="javascript" type="text/javascript"></script> tag
<script language="javascript" type="text/javascript">
function Setconfirm()
{
int number = confirm(“Changes have been saved to the profile.”)
if(number)
{
window.location = "Admin_User.aspx";
</script>
----------------------------------------------------------------------------------------------------------
for displaying alert textbox only with ok button
write code on aspx.cs file where u want to call confirmation box
ClientScript.RegisterStartupScript(GetType(), "jsScript", "Setconfirm();", true);

write Setconfirm() function on aspx file under <script language="javascript" type="text/javascript"></script> tag
function Setconfirm() {
alert("Changes have been saved to the profile.");
window.location = "Admin_User.aspx";
}
</script>

Monday, 4 July 2011

Grid View BoundColumn DataField

 create table [user](Created_Date datetime,Username varchar(50),Status varchar(20))
insert into [user] values(getdate(),'swapnil','smart boy')
insert into [user] values(getdate(),'swapnil','smart boy')
insert into [user] values(getdate(),'swapnil','smart boy')
insert into [user] values(getdate(),'swapnil','smart boy')
insert into [user] values(getdate(),'swapnil','smart boy')

create procedure gridbound
as
Begin
select * from [user]
End

-----------------------aspx page----------------------------

<form id="form1" runat="server">
<asp:DataGrid ID="grdStatusHistory" runat="server" AutoGenerateColumns="False" Height="33px" Width="277px">
<Columns>
<asp:BoundColumn DataField="Created_Date" HeaderText="Date">
</asp:BoundColumn>
<asp:BoundColumn DataField="Username" HeaderText="User">
</asp:BoundColumn>
<asp:BoundColumn DataField="Status" HeaderText="Action">
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>

------------------aspx.cs page----------------------------------
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
cls_PMPtables obj = new cls_PMPtables();
DataSet ds = new DataSet();
ds = obj.gridbind();
grdStatusHistory.DataSource = ds.Tables[0];
grdStatusHistory.DataBind();
}

--------------- cls_PMPtables .cs class file----------------------
-------Globally define connection cls_PMPtables .cs page
public SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings["statecity"]);
SqlCommand cmd = new SqlCommand();

public DataSet gridbind()
{
DataSet ds = new DataSet();
try
{
sqlconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "gridbound";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlconn;
SqlDataAdapter sqlda = new SqlDataAdapter();
sqlda.SelectCommand = cmd;
sqlda.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlconn.Close();
}
return ds;
}

------------------------web.config file----------------------------------------------
<appSettings>
<add key="statecity" value="Data Source=TECH-SWAPNILM\SQLEXPRESS; Initial Catalog=master; Integrated Security=true" />
</appSettings>

Friday, 1 July 2011


create table date(username varchar(50),password varchar(50), logindate datetime ,logoutdate datetime)

-----------loginpage.aspx.cs-----------
protected void Button1_Click(object sender, EventArgs e)
{
cls_PMPtables obj = new cls_PMPtables();
DataSet ds = new DataSet();
username = TextBox1.Text;
password = TextBox2.Text;
Session["username"] = username;
obj.savelogin(username,password);
if (ds != null)
{
Response.Redirect("Default2.aspx");
}
}
}

-------------------- cls_PMPtables.cs class file define globally connection----------------------------

public SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings["statecity"]);
SqlCommand cmd = new SqlCommand();
-----------------------------------------------------------------------------------------------
------------------ cls_PMPtables.cs class file--------------------------------------------------
public void savelogin( string username, string password)
{
try
{
sqlconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "PMP_Savelogin";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlconn;

cmd.Parameters.Add("@Username", SqlDbType.VarChar);
cmd.Parameters["@Username"].Value = username;

cmd.Parameters.Add("@Password", SqlDbType.VarChar);
cmd.Parameters["@Password"].Value = password;

cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlconn.Close();
}
}

------savelogin procedure---------
 
create PROCEDURE [dbo].[PMP_Savelogin]
@Username AS varchar(50),
@Password AS varchar(50)
AS
SELECT logindate FROM DATE WHERE CONVERT(VARCHAR(25),logindate,101)= CONVERT(VARCHAR(25),getDate(),101)
IF (@@rowcount = 0)
BEGIN
INSERT INTO date (username,password,logindate,logoutdate)
VALUES
(@Username,@Password,getdate(),getdate())
insert into Attendence(username,hourlytime,AttendenceDate)
values
(@Username,'',getdate())
END
ELSE
BEGIN
update date set username = @Username , password = @Password where username = @Username
END
---------------Default2.aspx.cs---------------
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["username"].ToString();
}
*****************************************************************

----------create table attendence fo storing hourlytime on particular date--------
create table attendence(username varchar(50), hourlytime varchar(10),AttendenceDate datetime)
-----------------------------------------------------------------------------------------------------

--------------create GetTime function for calculating login logout difference for particular day--------

Create FUNCTION [dbo].[GetTime](@username VARCHAR(50),@Date DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @hour AS VARCHAR(10),@minute AS VARCHAR(10)

SELECT @hour=datediff(hour,logindate,logoutdate) from date where username=@username AND CONVERT(VARCHAR(25),logindate,111) = CONVERT(VARCHAR(25),@Date,111)
SELECT @minute=datediff(minute,logindate,logoutdate) from date where username=@username AND CONVERT(VARCHAR(25),logindate,111) = CONVERT(VARCHAR(25),@Date,111)

RETURN (@hour + ':' + @minute)
END
--------------------------------------------------------------------------------------------------------------------------
--------------also Defaul2.aspx page take one hyperlink and set navigationurl property Logout.aspx--------
------------and write code on Logout.aspx.cs file page load-------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string user = Session["username"].ToString();
cls_PMPtables obj = new cls_PMPtables();
DataSet ds = new DataSet();
DateTime logoutdate = DateTime.Now;
obj.saveloginn(user,logoutdate);
}
}

-------------- cls_PMPtables.cs class file write saveloginn procedure---------
Create PROCEDURE [dbo].[PMP_Saveloginn]
@Username AS varchar(50),
@Logoutdate AS datetime
As
Declare @loginday as INT, @logoutday as INT
select @loginday = DATEPART(dy, logindate) from date where username = @Username
select @logoutday = DATEPART(dy, logoutdate) from date where username = @Username
if (@loginday = @logoutday )
BEGIN
update date set logoutdate = @Logoutdate where username=@Username and CONVERT(VARCHAR(25),logindate,101)= CONVERT(VARCHAR(25),getDate(),101)
update attendence set hourlytime = dbo.GetTime(@Username,getdate()),AttendenceDate=getdate()
where username = @Username and CONVERT(VARCHAR(25),AttendenceDate,101)= CONVERT(VARCHAR(25),getDate(),101)
END
else
Begin
Declare @password as varchar(50)
SELEct @password = password from date where username = @Username
insert into date (username,password,logindate,logoutdate) values(@Username,@password,getdate(),getdate())
end

-----------------------------------------------------------------------------