Tuesday, June 30, 2009

SQL Date Functions

here are some sql DATE related functions :

1. SET DATEFIRST 1 // set first day of the week as Monday

2. SELECT (datepart(ww,@tDate)) + 1- datepart(ww,dateadd(dd,-datepart(dd,@tDate),@tDate)) nWeek //Week number in a month for a specific date

3. SELECT DATEADD(mm, DATEDIFF(mm,0,@Month), 0) //First Day of the Month

4. SELECT DATEPART(WK, @Month) //Week Number of the Year

5. Select Current_Timestamp; // return current Select Current_Timestamp;

6. By using any of the following date functions it is possible to breakdown timestamps


SELECT MONTH(CURRENT_TIMESTAMP);- Return a Month(6)

SELECT DAY(CURRENT_TIMESTAMP);-Return a Day:(22)

SELECT DATE(CURRENT_TIMESTAMP); - returns a date (2004-06-22)

SELECT TIME(CURRENT_TIMESTAMP); - returns the time (10:33:11.840)

SELECT DAYOFWEEK(CURRENT_TIMESTAMP); - returns a numeric value (1-7)

SELECT DAYOFMONTH(CURRENT_TIMESTAMP); - returns a day of month (1-31)

SELECT DAYOFYEAR(CURRENT_TIMESTAMP); - returns the day of the year (1-365)

SELECT MONTHNAME(CURRENT_TIMESTAMP); - returns the month name (January - December)

SELECT DAYNAME(CURRENT_TIMESTAMP); - returns the name of the day (Sunday - Saturday)

SELECT WEEK(CURRENT_TIMESTAMP); - returns number of the week (1-53)

The LINQ Project

The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations.


It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities


LINQ Project

Textbox Validation Summary C#

A ValidationSummary control is displayed when the IsValid property of the page is false. It "polls" each of the validation controls on the page and aggregates the text messages exposed by each. The following sample illustrates displaying errors with a ValidationSummary.


Get Tex box Validation Summery

Monday, June 29, 2009

Delete File in ASP

Yes, you can delete a file by using FileSystem Object in ASP


<%
// delete file
var FSO = Server.CreateObject("Scripting.FileSystemObject");
// get path to file
PathFile = Server.MapPath("/path/folder/deletefilename);
// if file exists, delete it
if(FSO.FileExists(PathFile)) FSO.DeleteFile(sPathToFile)
// file deleted

%>


like this way we can create , delete folder using FSO (file system object)
to know more about FSO system

Tuesday, June 23, 2009

Printing using C#

Hi , printing is very easy case in all the task ,but when using ASP.NET, printing of web form is not that easy.
if we're building a UI we usually do so by painting controls on the screen and allowing whoever developed them to worry about the whys and wherefores about how that control should look.

If you want to print on server side, then you can use System.Drawing.Printing classes just like when you work with .Net Windows Forms application.
but if you are using client side then you need to consider more constraints and different possible solutions, depending of your specific needs.

For printing, you use the System.Drawing.Printing namespace. The main class in this namespace is the PrintDocument class, which represents an object that sends output to the printer. The role of this class is so central that you can achieve simple and complex printing tasks by using this class alone. However, as we shall see, other classes in this and other namespaces help make coding easier.
o print text or graphics, you call the Print method of the System.Drawing.Printing.PrintDocument class. One of the events the Print method invokes is the PrintPage event. You need to wire an event handler to the PrintPage event and write the code to send output to the printer. The event handler will receive an argument of type

Here are way to print the Document / Form.
Using PrintDialog

private void filePrintMenuItem_Click(Object sender , EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}



The filePrintMenuItem_Click event handler sends output to the printer if the user clicks the Print dialog box's OK button.

Page Setup
while priting adds the feature to set up the page that is used for printing.


1. private PageSettings pgSettings = new PageSettings();
2. Set pgSettings to the DefaultPageSettings property of printDoc before printing.

private void filePrintMenuItem_Click(Object sender , EventArgs e)
{
printDoc.DefaultPageSettings = pgSettings;
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}

3. Allow the user to change the setting of the page..

private void filePageSetupMenuItem_Click(Object sender , EventArgs e)
{
PageSetupDialog pageSetupDialog = new PageSetupDialog();
pageSetupDialog.PageSettings = pgSettings;
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.AllowMargins = true;
pageSetupDialog.ShowDialog();
}


4.Set the printDoc_PrintPage event handler to take into account the top and left margins.
private void printDoc_PrintPage(Object sender , PrintPageEventArgs e)
{
String textToPrint = ".NET Printing is easy";
Font printFont = new Font("Courier New", 12);
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black,
leftMargin, topMargin);
}




Print Preview
In a Windows application, before the user prints, they can normally view a preview of how the printout will like on paper. You can also provide this feature by adding the following code to the filePrintPreviewMenuItem_Click event handler.

private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e)
{
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = printDoc;
dlg.ShowDialog();
}





here you can get best printing
example
Hope it will helpful for you,plz comment on it.

Thank You,

Saturday, June 20, 2009

Open other website content on my asp.net Page

here is an Tips for displaying an Otherpage content on you'r own asp.net web page

follow this steps .

  • Add new Panel


  • <asp:panel id="ViewPanel runat="server" Width="100%" >
    </asp:panel>


  • Add Namespan


  • using System.Text;using System.Net;using System.IO;

    add this namespace on your page declaration part.

  • now on Page_load() paste this code



  • protected void Page_Load(object sender, EventArgs e)
    {
    Uri weburi = new Uri(http://www.gmail.com/);
    StringBuilder str = new StringBuilder();
    string temp = "";
    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(weburi);
    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
    StreamReader webstream = new StreamReader(webresponse.GetResponseStream(), Encoding.ASCII);
    while ((temp = webstream.ReadLine()) != null)
    str.Append(temp + "\r\n");
    webstream.Close();
    mainpanel.Controls.Add(new System.Web.UI.LiteralControl(str.ToString()));
    }




Plz write full path on you'r URI.
that's it

ways of using hyperlink control in data controls.

Here are some different ways of using hyperlink control in data controls.

What is a Hyperlink control:
A hyperlink control is used to navigate from the client to another page.Here is a example for a hyperlink control

<asp:HyperLink ID="hyp1" runat="server" Text="test" ></asp:HyperLink>


In data controls like datagrid and gridview we have hyperlink column and hyperlink field.
We will have a gridview containing hyperlinkfield as shown below where DataNavigateUrlFields and DataNavigateUrlFormatString are used to get
Fields from datasource and construct the url for navigation to next page.In the datagrid hyperlink column we have the same fields and you can follow the same procedure.


<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:HyperLinkField HeaderText="Category" DataTextField="CategoryName" DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="~/page.aspx?id={0}" />
</Columns>
</asp:GridView>

in code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}

public void BindGrid()
{
SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Persist Security Info=False ");
SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select * from emp", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
gdview.DataSource = ds.Tables[0].DefaultView;
gdview.DataBind();

}


Using Mutliple dataitems
If you want to send mutliple items to next page you have to do like this



<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:HyperLinkField HeaderText="Category" DataTextField="CategoryName" DataNavigateUrlFields="CategoryID,CategoryName" DataNavigateUrlFormatString="~/page.aspx?id={0}&name={1}" />
</Columns>
</asp:GridView>



In runtime
Inorder to access the hyperlinkfield in code behind we have to follow the below
Procedure

In design view:



<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" OnRowDataBound="gdview_RowDataBound" >
<Columns>
<asp:HyperLinkField HeaderText="Category" DataTextField="CategoryName" runat="server" />
</Columns>
</asp:GridView>



In code behind:

protected void gdview_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLinkField hyp = (HyperLinkField)gdview.Columns[0];
string[] items = new string[2];
items[0] = "CategoryName";
items[1] = "CategoryID";
hyp.DataNavigateUrlFields = items;
hyp.DataNavigateUrlFormatString ="~/page.aspx?id={0}&name={1}";
}



Using Hyperlink control

In the case of repeater and datalist we don’t have hyperlink column.In that scenario we have to use hyperlink control in itemtemplate.

.


In design view:
<asp:Repeater ID="rp1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink ID="hyp1" runat="server" Text='<%#Eval("CategoryName")%>' NavigateUrl='<%#"page.aspx?id=" + DataBinder.Eval(Container.DataItem," CategoryID") + "&name=" + DataBinder.Eval(Container.DataItem,"CategoryName")%>' ></asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>



in code behind:


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRep();
}
}

public void BindRep()
{
SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
rp1.DataSource = ds.Tables[0].DefaultView;
rp1.DataBind();
}


Here in this example we have passed multiple data items to page.aspx.In the same way you can create hyperlink control in datalist.

Friday, June 19, 2009

prevent multiple form submit at client side

In Html tag Button one issue is that they can be clicked more than once, and under certain circumstances this can cause issues. because of that may be data may get inserted multiple time, wrong information can be get added and so on. To ensure that the button can only be pressed once you can do the following.

Actually there may way to do this
1. Disable button

javascript:this.disabled=true;this.value='Submitting....';doSubmit('SUBMIT');

Simply add this in you button onClick method

<INPUT TYPE="Button" VALUE="Submit" onClick="if(this.value == 'Submit') this.form.submit(); this.value = 'Please Wait.';this.disabled=true;">

2. Count click

Second one is count how many times user get click on it


<script language="javascript">
var NoOfClick=0;
function chksubmit()
{
if NoOfClick >0)
{
alert("Wait! Request is in Process...");
return false;
}
NoOfClick ++;
document.creditcardinfo.action="xyz.asp";
document.creditcardinfo.submit();
}

<html>
<itle> button Ex <title>
<body> <form>
<input type="button" value="Submit" onClick="chksubmit();">
</form></body>
</html>


3. Hide button

in this section simply define one Div and in javascript hide or show that div section , so when user click on submit that div(consider loding images) will appear on Submit button and user will not able to view that submit button.like that way also you can stop user to clicking more than ones time.

hope you like my tricks
any query plz let me know?