Just Drag and drop Grid view control to your web page
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConvertDateReadertoTableUsingLoad();
}
private void ConvertDateReadertoTableUsingLoad()
{
SqlConnection conn = null;
string connString = "server=servername;database=databasename;uid=userid;password=password;";
conn = new SqlConnection(connString);
string query = "select * from Tablename";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
[/code]
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Confirmation message on button click in GridView?
Just Drag and drop Grid view control to your web page.
In Aspx.cs Page:-
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
SqlConnection conn = null;
string connString = "server=servername;database=databasename;uid=userid;password=password;";
conn = new SqlConnection(connString);
string query = "select * from TableName";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
conn.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView vDataRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button vBtnDelete = (Button)e.Row.FindControl("BtnDelete");
vBtnDelete.Attributes.Add("onclick", "return GridDelete()");
}
}
protected void BtnDelete_Click(object sender, EventArgs e)
{
Response.Write("Put your delete code here");
}
}
[/code]
In Aspx Page:-
[code]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<script type="text/javascript">
function GridDelete()
{
if (confirm("Are you sure to delete this record permanently?") == true)
{
return true;
}
else
{
return false;
}
}
</script>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server" Text="Delete"
onclick="BtnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
[/code]
In Aspx.cs Page:-
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
SqlConnection conn = null;
string connString = "server=servername;database=databasename;uid=userid;password=password;";
conn = new SqlConnection(connString);
string query = "select * from TableName";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
conn.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView vDataRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button vBtnDelete = (Button)e.Row.FindControl("BtnDelete");
vBtnDelete.Attributes.Add("onclick", "return GridDelete()");
}
}
protected void BtnDelete_Click(object sender, EventArgs e)
{
Response.Write("Put your delete code here");
}
}
[/code]
In Aspx Page:-
[code]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<script type="text/javascript">
function GridDelete()
{
if (confirm("Are you sure to delete this record permanently?") == true)
{
return true;
}
else
{
return false;
}
}
</script>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server" Text="Delete"
onclick="BtnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
[/code]
How to pass parameter value to Stored Procedure ?
Just Drag and drop one grid view control on your web page.
In Aspx Page:-
[code]
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim command As New SqlCommand
Dim StrConnection As New SqlConnection("server=servername;database=databasename;uid=userid;pwd=password;") 'Give your connection string
Dim myDataReader As SqlDataReader
Dim Res As New StringBuilder
Dim da As SqlDataAdapter
command.Connection = StrConnection
command.Connection.Open()
command.CommandText = "ReturnMultipleValues" ' Give your sp name
Command.CommandType = CommandType.StoredProcedure
command.Parameters.AddWithValue("@ParameterName1", "YourValue")
command.Parameters.AddWithValue("@ParameterName2", "YourValue")
Dim dt As New DataTable
myDataReader = command.ExecuteReader()
dt.Load(myDataReader)
End Sub
End Class
[/code]
In Aspx Page:-
[code]
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim command As New SqlCommand
Dim StrConnection As New SqlConnection("server=servername;database=databasename;uid=userid;pwd=password;") 'Give your connection string
Dim myDataReader As SqlDataReader
Dim Res As New StringBuilder
Dim da As SqlDataAdapter
command.Connection = StrConnection
command.Connection.Open()
command.CommandText = "ReturnMultipleValues" ' Give your sp name
Command.CommandType = CommandType.StoredProcedure
command.Parameters.AddWithValue("@ParameterName1", "YourValue")
command.Parameters.AddWithValue("@ParameterName2", "YourValue")
Dim dt As New DataTable
myDataReader = command.ExecuteReader()
dt.Load(myDataReader)
End Sub
End Class
[/code]
How to Create Text file using asp.net?
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Give your new file path
FileInfo t = new FileInfo("C:/WebSite10/test.txt");
StreamWriter Tex = t.CreateText();
//Enter your text with in writeline
Tex.WriteLine("How to create Text file using asp.net");
Tex.WriteLine("www.renganathan1984.blogspot.com");
//Add new line in your text file
Tex.Write(Tex.NewLine);
Tex.WriteLine("Trichy");
Tex.WriteLine("Thiruvellarai");
Tex.Close();
Response.Write("File Succesfully created!");
}
}
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Give your new file path
FileInfo t = new FileInfo("C:/WebSite10/test.txt");
StreamWriter Tex = t.CreateText();
//Enter your text with in writeline
Tex.WriteLine("How to create Text file using asp.net");
Tex.WriteLine("www.renganathan1984.blogspot.com");
//Add new line in your text file
Tex.Write(Tex.NewLine);
Tex.WriteLine("Trichy");
Tex.WriteLine("Thiruvellarai");
Tex.Close();
Response.Write("File Succesfully created!");
}
}
How to sort Datatable in C#
Abstract
Just Drag and drop the Grid view control to web page.
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(string));
table.Columns.Add("name", typeof(string));
table.Columns.Add("Offer", typeof(string));
table.Columns.Add("City", typeof(string));
DataRow row = null;
for (int i = 0; i < 5; i++)
{
row = table.NewRow();
row["Id"] = i;
row["name"] = "Ram";
row["Offer"] = "Offer";
row["City"] = "City Ram";
table.Rows.Add(row);
}
DataView dataView = new DataView(table);
dataView.Sort = "Id DESC";
//dataView.Sort = "Id DESC";
grvgrid.DataSource = dataView;
grvgrid.DataBind();
}
}
[/code]
How to get the ASCII value in C#
Abstract
Just Drag and drop the Text box and button control to your web page.In run time
give the input to Text box then click the button control the result will be display in your page.
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
string strInput ="";
string strTemp ="";
strInput = TextBox1.Text.ToString().Trim();
for (i = 0; i <= strInput.Length - 1; i++)
{
int charValue = Convert.ToInt32(strInput[i]); // converting in ascii
if (strTemp == "")
{
Response.Write(charValue);
Response.Write("
");
}
else
{
Response.Write(charValue);
Response.Write("
");
}
}
}
}
[/code]
Subscribe to:
Posts (Atom)