Aug 02

crystal report in asp.net

Hi,

I this post i will explain how to create a simple crystal report.

1.Open VS studio
2.open a new window application.
3.Drag and drop a Crystal report viewer from the tools( under Reporting).
4.Place a button in the form.Crystal Report
5.Double click on button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;namespace nn
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1_Click(object sender, EventArgs e)
{
ReportDocument rod = new ReportDocument();
rod.Load(“D:\\Naseer\\st\\nn\\CrystalReport1.rpt”);
crystalReportViewer1.ReportSource = rod;
crystalReportViewer1.Refresh();
}
}
}

 

Apr 03

how to filter dataset in asp.net using c#

Hi,

In this article I will explain you how to set filter in dataset while getting data from backend to frontend.

Observe the below sqlserver database table , In this we can see the pincode column were it contain different values in pincode column

DBImage

Eg:I want to get only picode=524004 for that I can write condition in database also but in some scenarios like dynamic sp’s, It won’t work at that scenario we can use this filter in code level

1.Add a page FilterInDS.aspx to solution

Copy and paste this Body in that page


<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />

<asp:GridView ID="gdvData" runat="server">

</asp:GridView>

</div>

</form>

</body>

Copy and paste below code in cs file


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 FilterinDS : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnGetData_Click(object sender, EventArgs e)

{

SqlConnection Connection = new SqlConnection("Server=Naseer-PC;Database=Naseer;Uid=sa;Pwd=123");

DataSet ds = new DataSet();

SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Mst_EmployeeDetails", Connection);

try

{

adapter.Fill(ds);

var strExpr = "PinCode = '" + 524004 + "'";

var dv = ds.Tables[0].DefaultView;

dv.RowFilter = strExpr;

var newDataTable = dv.ToTable();

DataSet ds1 = new DataSet();

ds1.Tables.Add(newDataTable);

if (newDataTable.Rows.Count > 0)

{

gdvData.DataSource = newDataTable;

gdvData.DataBind();

}

}

catch (Exception ex)

{

Connection.Close();

}

}

}

Execute the code and you will get filtered data based on pincode as below

DataBind

Happy Coding!!!!!

Admin.

Mar 30

how to export data from sqlserver database to excel in asp.net c#

Hi,

In this article I will explain you how to export data directly from sql server database table to excel,

Assume there is some data in the sql server database as below:

DB

I will export this data into excel file to excel by using C#.net coding as below:

1.Add a new file TabletoExcel.aspx in the solution explorer

Copy and paste below code in TabletoExcel.aspx page


<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="btnGenaerateToExcel" runat="server" onclick="btnGenerateToExcel_Click" Text="Button" />

</div>

</form>

</body>

Copy and paste below code in TabletoExcel.cs  page:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;

using System.Data;

public partial class TableToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnGenerateToExcel_Click(object sender, EventArgs e)
{
PopulatExcelEntry();
}

protected void PopulatExcelEntry()
{
SqlConnection Connection = new SqlConnection("Server=Naseer-PC;Database=Naseer;Uid=sa;Pwd=123");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Mst_Employee", Connection);

try
{
adapter.Fill(ds);
ExportToExcel(ds);
}

catch (Exception ex)
{
Connection.Close();
}
}

public static void ExportToExcel(System.ComponentModel.MarshalByValueComponent DataSource)

{
try
{
System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
System.Web.UI.WebControls.DataGrid tempDataGrid = new System.Web.UI.WebControls.DataGrid();
System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=TableToExcel.xls");
tempDataGrid.DataSource = DataSource;
tempDataGrid.DataBind();
tempDataGrid.HeaderStyle.Font.Bold = true;
tempDataGrid.RenderControl(objHtmlTextWriter);
DataSource.Dispose();
HttpContext.Current.Response.Write(objStringWriter.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}

catch (Exception ex)
{
throw ex;
}
}
}

Execute the code you will get this below screen after clicking Button

PopUp

Click on Ok and you will get your required output as:

ExcelData

Happy Coding!!!!

Admin.

Feb 22

How to convert mm-dd-yyyy to mm-yyyy in sql server

Hi,In this article I will going to explain you how to convert ‘MM-DD-yyyy’ to ‘MMM-yyyy’

Every developer will face problem while converting date-time, I am keeping query to convert Datetime

Eg:  (’12/01/2013 AM 12:00:00′)  to (‘Dec – 2013’)

Query:


SELECT LEFT(DATENAME(MONTH ,CONVERT(VARCHAR(10), '12/01/2013 AM 12:00:00',112)),3)

+' - '+ DATENAME(YEAR,CONVERT(VARCHAR(10), '12/01/2013 AM 12:00:00', 112))

O/p:-

Dec – 2013

 

 

Happy Coding!!!!!

Admin.

Dec 07

custom button in c# .net

Hi,In this article I will going to explain you about windows applications “Custom Button”

Custom Button : which is looks like WPF button representation, it consists of some extra futures then ordinary windows button,  In this button we can place images and also we can do different  shapes . It works as windows application button but including some extra features for rich look of application.

–>To use this CButton we have to include one of the library called “CButtonLib.dll”

–>You can download from here CButtonLib

Step1:

Add this custom library into our project

1)      In the Tool box of Windows application right click and Add Tab and Name it as ‘CButton’ or as your wish the name.

Pic1

2)      Right click on the CButton and Choose Items….

pic2

–>After this the dialog box appeared like below.

pic8

–>Click on the browse button and select DLL from the DLL saved Location as below

pic3

–>Click on the open,immediately the DLL  will be registered in .Net Frame work Components like below.

pic4

–>Then click OK. And CButton will appeared in the Tool Box like below.

pic5

–>After this we will work it as windows form button with included features.

pic6

–>O/p Screens:

pic7

Happy Coding!!!!

Admin.

Nov 30

how to generate a random password and send to mail in asp.net

Hi,In this article I will explain you how to generate auto generated password and also how to send to generated password mails IDS

–> In this application I used GMAIL Credentials to send mails ,use your own maild SMPTS to send mails

–> GMAIL  SMTP = smtp.gmail.com

–> GMAIL PORT =587

–> Add using System.Net.Mail;

–> I written a method ” getRandomPassword()”  to generate random password

Step1:

–> Add a new webform with name ” RandomPassword.aspx”

–> Copy and paste below code in RAndomPassword.aspx

Pic1


&nbsp;

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

<!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></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<fieldset>

<legend>Generate Random Password To Mail</legend>

<table>

<tr>

<td align="right">

Email ID :

</td>

<td align="left">

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td align="right">

</td>

<td align="left">

<asp:Button ID="btnGenerate" runat="server" Text="Generate"

onclick="btnGenerate_Click" />

</td>

</tr>

</table>

</fieldset>

</div>

</form>

</body>

</html>

Step 2:

–> Copy and paste the below code in RandomPassword.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net.Mail;

public partial class RandomPassword : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

private string getRandomPassword()

{

string allowedChars = "";

allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";

allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";

char[] sep = { ',' };

string[] arr = allowedChars.Split(sep);

string passwordString = "";

string temp = "";

Random rand = new Random();

for (int i = 0; i < 8; i++)

{

temp = arr[rand.Next(0, arr.Length)];

passwordString += temp;

}

return passwordString;

}

protected void btnGenerate_Click(object sender, EventArgs e)

{

SmtpClient client = new SmtpClient("smtp.gmail.com");

var message = new MailMessage();

client.Host = "smtp.gmail.com";

client.Port = 587;

client.UseDefaultCredentials = false;

client.Credentials = new System.Net.NetworkCredential("naseer526@gmail.com", "*******");

client.EnableSsl = true;

MailAddress SendFrom = new MailAddress("naseer526@gmail.com", "Naseer");

try

{

String password = getRandomPassword();

MailAddress SendTo = new MailAddress(txtEmail.Text);

message = new MailMessage(SendFrom, SendTo);

message.Subject = "Auto Generated Password";

message.Body = "Auto Generated Password has been generated, login using below creditiantials:<br/>" + "<br/>UserName :" + txtEmail.Text + "<br/>Password :" + password;

message.IsBodyHtml = true;

message.Priority = MailPriority.High;

client.ServicePoint.MaxIdleTime = 0;

client.ServicePoint.ConnectionLimit = 1;

client.Send(message);

ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Mail Sent successfully');</script>");

}

catch

{

ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Unexpected Error Occur TryAgain ');</script>");

}

}

}

O/P Screen:

pic2

Happy Coding!!!

Admin.

Nov 23

Insert Data in DataBase Using JQuery in ASP.NET

Hi,In this Article I will explain you how to Insert data into SQLServer using JQuery with  ASP.NET

If u want to insert so many columns then it’s so much of time to insert, to overcome this problem use JQuery  its very fast while executing,I will show you with example.

Add a New WebForm “JQuery.aspx”

–>Copy And Paste Below Code in it:


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Insert Data Using JQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {

$('#btnSubmit').click(function () {

$.ajax({

type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'JQuery.aspx/InsertDataUsingJQuery',
data: "{'FirstName':'" + document.getElementById('txtFirstName').value + "', 'LastName':'" + document.getElementById('txtLastName').value + "','Address':'" + document.getElementById('txtAddress').value + "','PhoneNo':'" + document.getElementById('txtPhoneNo').value + "','PinCode':'" + document.getElementById('txtPinCode').value + "'}",
async: false,
success: function (response) {
$('#txtFirstName').val('');
$('#txtLastName').val(''); $('#txtAddress').val('');
$('#txtPhoneNo').val('');
$('#txtPinCode').val('');

alert("Data Saved Sucessfully in Database");

},
error: function ()
{ console.log('there is some error'); }
});
});
});

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 40%">
<legend>Insert Data Using JQuery </legend>
<table width="100%">
<tr>
<td>
FirstName:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server" ClientIDMode="Static" Width="70%"></asp:TextBox>
</td>
<td>
LastName:
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server" ClientIDMode="Static" Width="70%"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" ClientIDMode="Static" Width="70%"></asp:TextBox>
</td>
<td>
PhoneNo:
</td>
<td>
<asp:TextBox ID="txtPhoneNo" runat="server" ClientIDMode="Static" Width="70%"></asp:TextBox>
</td>
</tr>
<tr>
<td>
PinCode:
</td>
<td>
<asp:TextBox ID="txtPinCode" runat="server" ClientIDMode="Static" Width="70%"></asp:TextBox>
</td>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ClientIDMode="Static" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>

–>Below Image shows the Design of form:

JQuery

–>Open “JQuery.cs” and Paste Below Code


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.Web.Services;
using System.Data;

public partial class JQuery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public static string InsertDataUsingJQuery(string FirstName, string LastName, string Address, string PhoneNo, string PinCode)
{
try
{
SqlConnection con = new SqlConnection("Server=Naseer-PC;Database=Naseer;Uid=sa;Pwd=123");
{
SqlCommand cmd = new SqlCommand("Insert into Tbl_Mst_EmployeeDetails(FirstName,LastName,Address,PhoneNo,PinCode) values('" + FirstName + "', '" + LastName + "','" + Address + "','" + PhoneNo + "','" + PinCode + "')", con);
{
con.Open();
cmd.ExecuteNonQuery();
return "True";
}
}
}
catch (Exception ex)
{
throw ex;
}

}
}

–>Create a Table Called Tbl_Mst_EmployeeDetails

–>Below Image shows the Structure of the table

JQuery2

o/p Screen:

JQuery3

Happy Coding!!!!!!

Admin.

Oct 27

how to alter a column data type which is a primary key in the table

Hi, In this article I will explain you how to alter a column data type which is a primary key in the table

–>We can’t alter the primary key column data type directly by using query

–>To do that we have to delete the primary key first and then we have to alter the column data type and then we have to set primary key

–>Here i am changing data type “Int” to “bigint”

I will show you how to do that with an example

Before EmpId column data type modification image

Alter

–>Tbl_Mst_EmployeeDetails is a Table Name

–>I am changing the EmpID column datatype

–This Query will drop the primary key temporarily


ALTER TABLE Tbl_Mst_EmployeeDetails

drop CONSTRAINT PK_Tbl_Mst_EmployeeDetails

— This Query will change data type


ALTER TABLE Tbl_Mst_EmployeeDetails

ALTER COLUMN EmpID BigInt not null

— This Query will add primary key


ALTER TABLE Tbl_Mst_EmployeeDetails

ADD CONSTRAINT PK_Tbl_Mst_EmployeeDetails PRIMARY KEY (EmpID)

o/p:After EmpId column data type modified image

Alter2

Happy Coding!!!!

Admin.

Oct 27

where are windows 7 wallpapers stored in system

Question:where are windows 7 wallpapers stored in system??

This a very common question in everyone’s mind for those who use windows7.

->Check this locations for  those pics

c:windowswebwallpaper

C:WindowsGlobalizationMCT

–Admin.

Oct 25

how to get second highest salary in sql

Hi,

In this article I will explain you a common interview question

Question : How you will get second highest salary from  Employee table

Ans:  Just simply Explain this below query and Scenario

Below Image shows you Employee table data

s1

Execute below query:


SELECT MAX(EmployeeSalary) AS SecondHighestSalary FROM Tbl_Mst_Employee

WHERE  EmployeeSalary < (SELECT MAX(EmployeeSalary) FROM Tbl_Mst_Employee)

o/p:

s2

 

Happy Coding!!!!

Admin.

 

Page 1 of 812345...Last »