(Note: I have use to connect the database in Mysql )
See below demo video for reference :
Click to play video
Screen Shot 2:
Screen Shot 3:
Screen Shot 4:
Codes:
Design Picture :
Fist you need to design the page like in Visual Studio 2010.
aspx code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="grnreceive.aspx.cs" Inherits="grnreceive" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style1
{
height: 42px;
}
</style>
<style type="text/css">
:focus {
background-color:white;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center> <h3> GRN RECEIVE </h3></center>
<center>
<table style="height: 61px; width: 393px">
<tr>
<td class="style1">
Enter Material Number</td>
<td class="style1">
<asp:TextBox ID="txtsrch" runat="server" Height="25px" Width="127px"></asp:TextBox>
</td>
<td class="style1">
<asp:Button ID="bnadd" runat="server" Text="Add" onclick="bnadd_Click"
Height="30px" style="font-size: large" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblerror" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</center>
<br />
<br />
<center>
<asp:GridView ID="grdvw" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkboxalldelete" runat="server" AutoPostBack="true" OnCheckedChanged="checkrow" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checkdelete" runat="server" AutoPostBack="true" /></ItemTemplate>
<HeaderStyle HorizontalAlign="Justify" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
<br />
<br />
<center>
<asp:Button ID="bnsave" runat="server" Text="Save" Height="30px" style="font-size: large" onclick="bnsave_Click" />
<br />
</center>
<br />
<br />
</asp:Content>
C# code:
(Rename it your lables , textboxs,buttons,gridviews)
Like Below:
label1=lblerror,
Textbox=txtsrch,
Gridview1=grvw,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Globalization;
using System.Configuration;
public partial class grnreceive : System.Web.UI.Page
{
string createdon;
string ipaddr;
DataTable tbs = new DataTable();
DataRow drow;
protected void Page_Load(object sender, EventArgs e)
{
string strHostName = null;
strHostName = System.Net.Dns.GetHostName();
ipaddr = System.Net.Dns.GetHostEntry(strHostName).AddressList[0].ToString();
createdon = DateTime.Now.ToString("yyyy-MM-dd");
lblerror.Visible = false;
if(!IsPostBack)
{
txtsrch.Focus();
create_table();
}
}
protected void create_table()
{
tbs.Columns.Add("GRNNO", typeof(string));
tbs.Columns.Add("GRN DATE", typeof(string));
tbs.Columns.Add("DEPARTMENT NAME", typeof(string));
tbs.Columns.Add("PROBLEM METERIAL DETAILS", typeof(string));
tbs.Columns.Add("PROBLEM", typeof(string));
tbs.Columns.Add("STAND BY METERIAL DETAILS", typeof(string));
tbs.Columns.Add("STAFF NAME", typeof(string));
tbs.Columns.Add("TRACK ITEM", typeof(string));
grdvw.DataSource = tbs;
grdvw.DataBind();
ViewState["table"] = tbs;
}
protected void bnadd_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
MySqlConnection myConnection = new MySqlConnection(connString);
myConnection.Open();
tbs = (DataTable)ViewState["table"];
drow = tbs.NewRow();
MySqlCommand cmd = new MySqlCommand("select grnno,grndate,deptname,pmdetails,problem,standby,staffname,trackitem from grntable where trackitem='DSR' and grnno='" +txtsrch.Text + "'", myConnection);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string grn = dr["grnno"].ToString();
string grn1 = dr["grndate"].ToString();
string grn2 = dr["deptname"].ToString();
string grn3 = dr["pmdetails"].ToString();
string grn4 = dr["problem"].ToString();
string grn5 = dr["standby"].ToString();
string grn6 = dr["staffname"].ToString();
string grn7 = dr["trackitem"].ToString();
string grn8 = txtsrch.Text;
drow["GRNNO"] = grn;
drow["GRN DATE"] = grn1;
drow["DEPARTMENT NAME"] = grn2;
drow["PROBLEM METERIAL DETAILS"] = grn3;
drow["PROBLEM"] = grn4;
drow["STAND BY METERIAL DETAILS"] = grn5;
drow["STAFF NAME"] = grn6;
drow["TRACK ITEM"] = grn7;
tbs.Rows.Add(drow);
grdvw.DataSource = tbs;
grdvw.DataBind();
txtsrch.Text = "";
myConnection.Close();
lblerror.Visible = true;
lblerror.Text="Sucessfully Added,Try Another";
}
else
{
lblerror.Visible = true;
lblerror.Text = "Invalid Material Number";
}
}
protected void bnsave_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
MySqlConnection myConnection = new MySqlConnection(connString);
myConnection.Open();
MySqlTransaction tran = myConnection.BeginTransaction();
try
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkdelete = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
string matno = grdvw.Rows[i].Cells[1].Text;
if (chkdelete.Checked == true)
{
MySqlCommand cmd1 = new MySqlCommand("update grntable set trackitem='GSC',modifiedon='" + createdon + "',modifiedby='" + txtsrch.Text + "',modifiedat='" + ipaddr + "' where grnno='" + matno + "'", myConnection, tran);
cmd1.ExecuteNonQuery();
}
}
tran.Commit();
myConnection.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Scucess')", true);
}
catch (Exception)
{
tran.Rollback();
myConnection.Close();
}
myConnection.Close();
Response.Redirect("grnreceive.aspx");
}
protected void checkrow(object sender, EventArgs e)
{
CheckBox chkheader = (CheckBox)grdvw.HeaderRow.FindControl("checkboxalldelete");
if (chkheader.Checked == true)
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkrr = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
chkrr.Checked = true;
}
}
else
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkrr = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
chkrr.Checked = false;
}
}
}
}
Database :
To create database :
create database jkt;
Open particular database :
use jkt;
Create a new table:
create table grntable
(
grnno int(11) not null auto_increment primary key ,
grndate date,
deptname varchar(100),
pmdetails varchar(250),
problem varchar(200),
standby varchar(10),
staffname varchar(50),
trackitem varchar(10)
);
Description for the table :
Screen Shot 3:
Screen Shot 4:
Codes:
Design Picture :
Fist you need to design the page like in Visual Studio 2010.
aspx code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="grnreceive.aspx.cs" Inherits="grnreceive" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style1
{
height: 42px;
}
</style>
<style type="text/css">
:focus {
background-color:white;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center> <h3> GRN RECEIVE </h3></center>
<center>
<table style="height: 61px; width: 393px">
<tr>
<td class="style1">
Enter Material Number</td>
<td class="style1">
<asp:TextBox ID="txtsrch" runat="server" Height="25px" Width="127px"></asp:TextBox>
</td>
<td class="style1">
<asp:Button ID="bnadd" runat="server" Text="Add" onclick="bnadd_Click"
Height="30px" style="font-size: large" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblerror" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</center>
<br />
<br />
<center>
<asp:GridView ID="grdvw" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkboxalldelete" runat="server" AutoPostBack="true" OnCheckedChanged="checkrow" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checkdelete" runat="server" AutoPostBack="true" /></ItemTemplate>
<HeaderStyle HorizontalAlign="Justify" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
<br />
<br />
<center>
<asp:Button ID="bnsave" runat="server" Text="Save" Height="30px" style="font-size: large" onclick="bnsave_Click" />
<br />
</center>
<br />
<br />
</asp:Content>
C# code:
(Rename it your lables , textboxs,buttons,gridviews)
Like Below:
label1=lblerror,
Textbox=txtsrch,
Gridview1=grvw,
Button=grdvw.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Globalization;
using System.Configuration;
public partial class grnreceive : System.Web.UI.Page
{
string createdon;
string ipaddr;
DataTable tbs = new DataTable();
DataRow drow;
protected void Page_Load(object sender, EventArgs e)
{
string strHostName = null;
strHostName = System.Net.Dns.GetHostName();
ipaddr = System.Net.Dns.GetHostEntry(strHostName).AddressList[0].ToString();
createdon = DateTime.Now.ToString("yyyy-MM-dd");
lblerror.Visible = false;
if(!IsPostBack)
{
txtsrch.Focus();
create_table();
}
}
protected void create_table()
{
tbs.Columns.Add("GRNNO", typeof(string));
tbs.Columns.Add("GRN DATE", typeof(string));
tbs.Columns.Add("DEPARTMENT NAME", typeof(string));
tbs.Columns.Add("PROBLEM METERIAL DETAILS", typeof(string));
tbs.Columns.Add("PROBLEM", typeof(string));
tbs.Columns.Add("STAND BY METERIAL DETAILS", typeof(string));
tbs.Columns.Add("STAFF NAME", typeof(string));
tbs.Columns.Add("TRACK ITEM", typeof(string));
grdvw.DataSource = tbs;
grdvw.DataBind();
ViewState["table"] = tbs;
}
protected void bnadd_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
MySqlConnection myConnection = new MySqlConnection(connString);
myConnection.Open();
tbs = (DataTable)ViewState["table"];
drow = tbs.NewRow();
MySqlCommand cmd = new MySqlCommand("select grnno,grndate,deptname,pmdetails,problem,standby,staffname,trackitem from grntable where trackitem='DSR' and grnno='" +txtsrch.Text + "'", myConnection);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string grn = dr["grnno"].ToString();
string grn1 = dr["grndate"].ToString();
string grn2 = dr["deptname"].ToString();
string grn3 = dr["pmdetails"].ToString();
string grn4 = dr["problem"].ToString();
string grn5 = dr["standby"].ToString();
string grn6 = dr["staffname"].ToString();
string grn7 = dr["trackitem"].ToString();
string grn8 = txtsrch.Text;
drow["GRNNO"] = grn;
drow["GRN DATE"] = grn1;
drow["DEPARTMENT NAME"] = grn2;
drow["PROBLEM METERIAL DETAILS"] = grn3;
drow["PROBLEM"] = grn4;
drow["STAND BY METERIAL DETAILS"] = grn5;
drow["STAFF NAME"] = grn6;
drow["TRACK ITEM"] = grn7;
tbs.Rows.Add(drow);
grdvw.DataSource = tbs;
grdvw.DataBind();
txtsrch.Text = "";
myConnection.Close();
lblerror.Visible = true;
lblerror.Text="Sucessfully Added,Try Another";
}
else
{
lblerror.Visible = true;
lblerror.Text = "Invalid Material Number";
}
}
protected void bnsave_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
MySqlConnection myConnection = new MySqlConnection(connString);
myConnection.Open();
MySqlTransaction tran = myConnection.BeginTransaction();
try
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkdelete = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
string matno = grdvw.Rows[i].Cells[1].Text;
if (chkdelete.Checked == true)
{
MySqlCommand cmd1 = new MySqlCommand("update grntable set trackitem='GSC',modifiedon='" + createdon + "',modifiedby='" + txtsrch.Text + "',modifiedat='" + ipaddr + "' where grnno='" + matno + "'", myConnection, tran);
cmd1.ExecuteNonQuery();
}
}
tran.Commit();
myConnection.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Scucess')", true);
}
catch (Exception)
{
tran.Rollback();
myConnection.Close();
}
myConnection.Close();
Response.Redirect("grnreceive.aspx");
}
protected void checkrow(object sender, EventArgs e)
{
CheckBox chkheader = (CheckBox)grdvw.HeaderRow.FindControl("checkboxalldelete");
if (chkheader.Checked == true)
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkrr = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
chkrr.Checked = true;
}
}
else
{
for (int i = 0; i < grdvw.Rows.Count; i++)
{
CheckBox chkrr = (CheckBox)grdvw.Rows[i].Cells[0].FindControl("checkdelete");
chkrr.Checked = false;
}
}
}
}
Database :
To create database :
create database jkt;
Open particular database :
use jkt;
Create a new table:
create table grntable
(
grnno int(11) not null auto_increment primary key ,
grndate date,
deptname varchar(100),
pmdetails varchar(250),
problem varchar(200),
standby varchar(10),
staffname varchar(50),
trackitem varchar(10)
);
Description for the table :
desc grntable;
Select all values from table :
select * from grntable;
Insert values into the table:
insert into grntable (grnno,grndate,deptname,pmdetails,problem,standby,staffname,trackitem)
values
(01,now(),'Hardware','Mouse','Click button not working','yes','John','DSR
'),
(02,now(),'Network','Printer','Paper not working','yes','Jagan','DSR
'),
(032,now(),'Hardware','Keyboard','Keys not working','yes','Sathish','DSR
');
Picture for table Structure:
No comments:
Post a Comment