Wednesday, December 21, 2011

Dump file for exam

Get Dump File


http://www.examcollection.com/microsoft_exams.html

Page Popup

First Pagre


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"%>

<html xmlns="http://www.w3.org/1999/xhtml"%>
<head runat="server"%>
%<title%>Untitled Page%</title%>
</head%>
<body%>
<form id="form1" runat="server"%>
<div>
Click Here To open popup Window
<br />
<asp:Button ID="btnPopup" runat="server" Text="Pop Up" /%>
</div%>
</form%>
</body%>
</html%>

Codebehind Page


public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnPopup.Attributes.Add("onclick", "window.open('Child.aspx',null,'left=200, top=30, height=550,width= 500, status=no, resizable= yes, scrollbars= yes, toolbar= no,location= no, menubar= no');");
}
}

Popup Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="child.aspx.cs" Inherits="child" %%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"%>

<html xmlns="http://www.w3.org/1999/xhtml"%>
<head runat="server"%>
<title>Untitled Page</head%>
<body>
<form id="form1" runat="server"%>
<div%>
This is is a child Page.
<asp:Button ID="btnClose" runat="server" Text="Close" /%>
</div%>
</form%>
</body%>
</html%>

Codebehind Page -To close the window


public partial class child : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnClose.Attributes.Add("onclick","window.opener.location.reload();window.close()");
}
}

Tuesday, December 13, 2011

Simple Registration form

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Signup.aspx.cs" Inherits="Signup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<title>Sample Registration Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td>
Full Name:</td>
<td>
<asp:TextBox ID="TxtName" runat="server"><asp:TextBox>
</td>
</tr>
<tr>
<td>
Username:</td>
<td>
<asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:</td>
<td>
<asp:TextBox ID="TxtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Re Password:</td>
<td>
<asp:TextBox ID="TxtRePassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address:</td>
<td>
<asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Age:</td>
<td>
<asp:TextBox ID="TxtAge" runat="server"></asp:TextBox>

</td>
</tr>
<tr>
<td>
Gender:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
</form>
</body>
</html>

As you can see, the UI is very simple. Now let’s set up the connection string.

STEP3: Setting up the Connection String

In your web.config file set up the connection string there as shown below:



<connectionStrings>
<add name="MyConsString" connectionString="Data Source=WPHVD185022-9O0;Initial Catalog=MyDatabase;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>



Note: MyConsString is the name of the Connection String that we can use as a reference in our codes for setting the connection string later.

STEP4: Calling up the ConnectionString in our codes

Here’s the method for calling the connection string that was set up in the web.config file.

public string GetConnectionString()

{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
}



STEP5: Writing the method for inserting the data from the registration page to the database.



In this demo, we are using the ADO.NET objects for manipulating the data from the page to the database. If you are not familiar with ADO.NET then I would suggest you to refer the following link below to get started:

ADO.NET Tutorial

Here’s the code block for inserting the data to the database.



private void ExecuteInsert(string name, string username, string password, string gender, string age, string address)

{
SqlConnection conn = new SqlConnection(GetConnectionString());

string sql = "INSERT INTO tblRegistration (Name, UserName, Password, Gender, Age, Address) VALUES "
+ " (@Name,@UserName,@Password,@Gender,@Age,@Address)";

try

{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[6];


//param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
param[3] = new SqlParameter("@Gender", SqlDbType.Char, 10);
param[4] = new SqlParameter("@Age", SqlDbType.Int, 100);
param[5] = new SqlParameter("@Address", SqlDbType.VarChar, 50);

param[0].Value = name;
param[1].Value = username;
param[2].Value = password;
param[3].Value = gender;
param[4].Value = age;
param[5].Value = address;

for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);

}
finally
{
conn.Close();
}
}


STEP6: Calling the method ExecuteInsert()

You can call the method above at Button_Click event for saving the data to the database. Here’s the code block below:

protected void Button1_Click(object sender, EventArgs e)
{
if (TxtPassword.Text == TxtRePassword.Text)
{
//call the method to execute insert to the database

ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, DropDownList1.SelectedItem.Text, TxtAge.Text, TxtAddress.Text);

Response.Write("Record was successfully added!");

ClearControls(Page);
}
else
{
Response.Write("Password did not match");
TxtPassword.Focus();
}
}

As you can see from the above code block, we check the value of the TxtPassword and TxtRePassword to see if match. If it match then call the method ExecuteInsert else display the error message stating that the “Password did not match”.

You also noticed that we call the method ClearControls for clearing the Text fields in the page. See the code block below for the ClearControls method:

public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}

That’s it! Hope you will find this example useful!


You can also create procedure and do the same thing in this way : Example



string project = ((TextBox)taskinsert.FooterRow.FindControl("addproject")).Text;
string task = ((TextBox)taskinsert.FooterRow.FindControl("addTask")).Text;
string des = ((TextBox)taskinsert.FooterRow.FindControl("addDescrip")).Text;
string empcode = ((TextBox)taskinsert.FooterRow.FindControl("addMember")).Text;
string status = "0";
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Enigma"].ToString().Trim());
SqlCommand cmd = new SqlCommand("addtask", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@empcode", SqlDbType.NVarChar).Value = empcode;
cmd.Parameters.Add("@project", SqlDbType.NVarChar).Value = project;
cmd.Parameters.Add("@task", SqlDbType.VarChar).Value = task;
cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = des;
cmd.Parameters.Add("@stat", SqlDbType.VarChar).Value = status;
con.Open();
int j = cmd.ExecuteNonQuery();
if (j > 0)
{
lblgridmsg.Text = "Task Inserted successfully";
project = "";
task= "";
des = "";
empcode = "";
}

Monday, December 5, 2011

select Table from xml

If we are using XML Data type then we often need to get table from XML. We will see here how to get table from XML data type. Lets take the XML with attribute and child tags in a variable @UsersDetail

select Table from xml

So we saw how easy to get data in a table format from XML, just need to use
'@attributename' to get attribute value
'childtagname[1]' to get value of tag

Now what if there is a root tag suppose users?
We just need to pass path of tag we are trying to get the value of means user, have a look here

select Table from xml

Delete Duplicate data

Hey there everyone, its been awhile since i have blogged hope everyone has been doing good.
We are here to look into problem related to duplicate records, suppose we have a table like

CREATE TABLE TestTable(COL1 INT, COL2 INT, COL3 VARCHAR(50))

INSERT INTO TestTable VALUES (1, 1, 'ONE')
INSERT INTO TestTable VALUES (1, 2, 'TWO')
INSERT INTO TestTable VALUES (1, 2, 'TWO')
INSERT INTO TestTable VALUES (1, 3, 'THREE')
INSERT INTO TestTable VALUES (1, 3, 'THREE')
INSERT INTO TestTable VALUES (1, 3, 'THREE')

SELECT * FROM TestTable



As we can see there are duplicate records, now what if we need to delete all duplicate records.
Here is an easy way to get it done, Create a CTE(Common Table Extension) of ROW_NUMBER using PARTITION BY from TestTable and delete all records from it with Row_Number > 1.
Have a look in code snippet

;WITH TestCTE AS(
SELECT ROW_NUMBER()OVER( PARTITION BY COL1, COL2, COL3 ORDER BY(SELECT '')) AS RowNumber
FROM TestTable)

DELETE TestCTE WHERE RowNumber > 1


Now you will see records have been deleted from original table as well, This is what we were looking for :)



Another Way of Delete Duplicate records



Create Table With Autogenerate ID Such As FirstName ,ID Columns .
This Table Contain Duplicate Records.

SELECT FirstName FROM tblTest GROUP BY FirstName HAVING COUNT(*) > 1

Then Delete

DELETE FROM tblTest WHERE ID NOT IN (SELECT MAX(ID) FROM tblTest GROUP BY [FirstName])

swap tabls column

declare @temp as int
update swap set @temp=newid,newid=id,id=@temp


int--datatype must be same for both columns

swap-table name
newid,id -column name