Monday, January 30, 2012

GridViewEditing SelectedRow

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

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">

<asp:Label ID="Label13" runat="server" ForeColor="Red" Text="Label">asp:Label>

<div>

<asp:Label ID="Label1" runat="server" Text="Customer Name">asp:Label>

<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>

<asp:Label ID="Label2" runat="server" Text="Customer Address">asp:Label>

<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"

ontextchanged="TextBox2_TextChanged">asp:TextBox>

<asp:Label ID="Label3" runat="server" Text="Invoice Number">asp:Label>

<asp:TextBox ID="TextBox3" runat="server">asp:TextBox>

<br />

<br />

<br />

<asp:Label ID="Label4" runat="server" Text="Invoice Date">asp:Label>

<span lang="en-us">

<asp:TextBox ID="TextBox4" runat="server">asp:TextBox>

<asp:Label ID="Label5" runat="server" Text="Product Name">asp:Label>

<asp:TextBox ID="TextBox5" runat="server">asp:TextBox>

<asp:Label ID="Label6" runat="server" Text="Product Description">asp:Label>

<asp:TextBox ID="TextBox6" runat="server">asp:TextBox>

<br />

<br />

<asp:Label ID="Label7" runat="server" Text="Unit Cost">asp:Label>

<asp:TextBox ID="TextBox7" runat="server">asp:TextBox>

<asp:Label ID="Label8" runat="server" Text="Quantity">asp:Label>

<asp:TextBox ID="TextBox8" runat="server">asp:TextBox>

<asp:Label ID="Label9" runat="server" Text="Total Amount to be paid">asp:Label>

<asp:TextBox ID="TextBox9" runat="server">asp:TextBox>

<br />

<br />

<asp:Label ID="Label10" runat="server" Text="Total Amount paid">asp:Label>

<asp:TextBox ID="TextBox10" runat="server">asp:TextBox>

<asp:Label ID="Label12" runat="server" Text="Tax Amount">asp:Label>

<asp:TextBox ID="TextBox12" runat="server">asp:TextBox>

<asp:Label ID="Label11" runat="server" Text="Comments">asp:Label>

<asp:TextBox ID="TextBox11" runat="server" TextMode="MultiLine">asp:TextBox>

<br />

<asp:Button ID="Button1" runat="server" Text="Add Invoice"

onclick="Button1_Click" style="height: 26px" />

<asp:Button ID="Button2" runat="server" Text="update Invoice"

onclick="Button2_Click" />

<asp:Button ID="Button3" runat="server" Text="Delete Invoice"

onclick="Button3_Click" />

span>

div>

<asp:GridView ID="DataGrid1" runat="server"

onselectedindexchanged="DataGrid1_SelectedIndexChanged">

<Columns>

<asp:ButtonField CommandName="Select" Text="Select"/>

Columns>

asp:GridView>

form>

body>

html>

CodeBehind

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 System.Data.SqlClient;

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

{

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = "Data Source=KSS-PC\\KSS;AttachDbFilename=D:\\projects\\QuestPond\\2_InvoicingApplicationWithValidations\\App_Data\\Invoicing.mdf;Integrated Security=True;Connect Timeout=30";

ObjConnection.Open();

// loading data in to datagrid

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice", ObjConnection);

objDataAdapter.Fill(objdataset);

// load the data set in to the data grid

DataGrid1.DataSource = objdataset;

DataGrid1.DataBind();

ObjConnection.Close();

}

protected void Button1_Click(object sender, EventArgs e)

{

// Start of validations

int resultvalue = 0;

if (TextBox1.Text.Length == 0)

{

Label13.Text = "Customer name is compulsory";

return;

}

if (TextBox3.Text.Length == 0)

{

Label13.Text = "Invoice code is compulsory";

return;

}

if (int.TryParse(TextBox8.Text, out resultvalue) == false)

{

Label13.Text = "Quantity should be numeric";

return;

}

if (Convert.ToInt16(TextBox8.Text) < 1)

{

Label13.Text = "Quantity should be atleast one";

return;

}

if (TextBox7.Text.Length == 0)

{

Label13.Text = "Unit cost is compulsory";

return;

}

if (TextBox5.Text.Length == 0)

{

Label13.Text = "Product name is compulsory";

return;

}

if (TextBox12.Text.Length == 0)

{

TextBox12.Text = "0";

}

if (TextBox10.Text.Length == 0)

{

TextBox10.Text = "0";

}

if (TextBox9.Text.Length == 0)

{

TextBox9.Text = "0";

}

// end of validations

// insert

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = "Data Source=KSS-PC\\KSS;AttachDbFilename=D:\\projects\\QuestPond\\2_InvoicingApplicationWithValidations\\App_Data\\Invoicing.mdf;Integrated Security=True;Connect Timeout=30";

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "Insert into invoice(InvoiceReference,InvoiceComments,InvoiceDate,ProductName,Description,Qty,Amount,TaxAmount,PaidAmount,CustomerName,CustomerAddress,UnitCost) values('" + TextBox3.Text + "','" + TextBox11.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "'," + TextBox8.Text + "," + TextBox9.Text + "," + TextBox12.Text + "," + TextBox10.Text + ",'" + TextBox1.Text + "','" + TextBox2.Text + "'," + TextBox7.Text + ")";

objCommand.ExecuteNonQuery();

// clearing of text boxes

TextBox3.Text = "";

TextBox11.Text = "";

TextBox4.Text = "";

TextBox5.Text = "";

TextBox6.Text = "";

TextBox8.Text = "";

TextBox9.Text = "";

TextBox12.Text = "";

TextBox10.Text = "";

TextBox1.Text = "";

TextBox2.Text = "";

TextBox7.Text = "";

// loading data in to datagrid

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice", ObjConnection);

objDataAdapter.Fill(objdataset);

// load the data set in to the data grid

DataGrid1.DataSource = objdataset;

DataGrid1.DataBind();

ObjConnection.Close();

}

protected void TextBox2_TextChanged(object sender, EventArgs e)

{

}

protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)

{

DataSet objDataSet = new DataSet();

SqlDataAdapter objDataAdapter;

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = "Data Source=KSS-PC\\KSS;AttachDbFilename=D:\\projects\\QuestPond\\2_InvoicingApplicationWithValidations\\App_Data\\Invoicing.mdf;Integrated Security=True;Connect Timeout=30";

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "Select * from invoice where InvoiceReference='" + DataGrid1.SelectedRow.Cells[1].Text + "'";

objDataAdapter = new SqlDataAdapter(objCommand);

objDataAdapter.Fill(objDataSet);

ObjConnection.Close();

TextBox3.Text = objDataSet.Tables[0].Rows[0][0].ToString();

TextBox11.Text = objDataSet.Tables[0].Rows[0][1].ToString();

TextBox4.Text = objDataSet.Tables[0].Rows[0][2].ToString();

TextBox5.Text = objDataSet.Tables[0].Rows[0][3].ToString();

TextBox6.Text = objDataSet.Tables[0].Rows[0][4].ToString();

TextBox8.Text = objDataSet.Tables[0].Rows[0][5].ToString();

TextBox9.Text = objDataSet.Tables[0].Rows[0][6].ToString();

TextBox12.Text = objDataSet.Tables[0].Rows[0][7].ToString();

TextBox10.Text = objDataSet.Tables[0].Rows[0][8].ToString();

TextBox1.Text = objDataSet.Tables[0].Rows[0][9].ToString();

TextBox2.Text = objDataSet.Tables[0].Rows[0][10].ToString();

TextBox7.Text = objDataSet.Tables[0].Rows[0][11].ToString();

}

protected void Button2_Click(object sender, EventArgs e)

{

// Start of validations

int resultvalue = 0;

if (TextBox1.Text.Length == 0)

{

Label13.Text = "Customer name is compulsory";

return;

}

if (TextBox3.Text.Length == 0)

{

Label13.Text = "Invoice code is compulsory";

return;

}

if (int.TryParse(TextBox8.Text, out resultvalue) == false)

{

Label13.Text = "Quantity should be numeric";

return;

}

if (Convert.ToInt16(TextBox8.Text) < 1)

{

Label13.Text = "Quantity should be atleast one";

return;

}

if (TextBox7.Text.Length == 0)

{

Label13.Text = "Unit cost is compulsory";

return;

}

if (TextBox5.Text.Length == 0)

{

Label13.Text = "Product name is compulsory";

return;

}

if (TextBox12.Text.Length == 0)

{

TextBox12.Text = "0";

}

if (TextBox10.Text.Length == 0)

{

TextBox10.Text = "0";

}

if (TextBox9.Text.Length == 0)

{

TextBox9.Text = "0";

}

// end of validations

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = "Data Source=KSS-PC\\KSS;AttachDbFilename=D:\\projects\\QuestPond\\2_InvoicingApplicationWithValidations\\App_Data\\Invoicing.mdf;Integrated Security=True;Connect Timeout=30";

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "update invoice set InvoiceComments='" + TextBox11.Text + "',InvoiceDate='" + TextBox4.Text + "',ProductName='" + TextBox5.Text + "',Description='" + TextBox6.Text + "',Qty=" + TextBox8.Text + ",Amount=" + TextBox9.Text + ",TaxAmount=" + TextBox12.Text + ",PaidAmount=" + TextBox10.Text + ",CustomerName='" + TextBox1.Text + "',CustomerAddress='" + TextBox2.Text + "',UnitCost=" + TextBox7.Text + "where InvoiceReference='" + TextBox3.Text + "'";

objCommand.ExecuteNonQuery();

// loading data in to datagrid

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice", ObjConnection);

objDataAdapter.Fill(objdataset);

// load the data set in to the data grid

DataGrid1.DataSource = objdataset;

DataGrid1.DataBind();

TextBox3.Text = "";

TextBox11.Text = "";

TextBox4.Text = "";

TextBox5.Text = "";

TextBox6.Text = "";

TextBox8.Text = "";

TextBox9.Text = "";

TextBox12.Text = "";

TextBox10.Text = "";

TextBox1.Text = "";

TextBox2.Text = "";

TextBox7.Text = "";

ObjConnection.Close();

}

protected void Button3_Click(object sender, EventArgs e)

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = "Data Source=KSS-PC\\KSS;AttachDbFilename=D:\\projects\\QuestPond\\2_InvoicingApplicationWithValidations\\App_Data\\Invoicing.mdf;Integrated Security=True;Connect Timeout=30";

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "delete from invoice where InvoiceReference='" + TextBox3.Text + "'";

objCommand.ExecuteNonQuery();

// loading data in to datagrid

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice", ObjConnection);

objDataAdapter.Fill(objdataset);

// load the data set in to the data grid

DataGrid1.DataSource = objdataset;

DataGrid1.DataBind();

TextBox3.Text = "";

TextBox11.Text = "";

TextBox4.Text = "";

TextBox5.Text = "";

TextBox6.Text = "";

TextBox8.Text = "";

TextBox9.Text = "";

TextBox12.Text = "";

TextBox10.Text = "";

TextBox1.Text = "";

TextBox2.Text = "";

TextBox7.Text = "";

ObjConnection.Close();

}

}



No comments: