You are on page 1of 14

Search Page

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Lbl_Error.Visible = false; AutoAdjustColumnWidth(); } LBtn_Modify.Enabled = false; IBtn_Modify.Enabled = false; IBtn_Delete.Enabled = false; LBtn_Delete.Enabled = false; } protected void Item_Master_GridView_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = Item_Master_GridView.SelectedRow; Lbl_Cookie.Text = row.Cells[1].Text; Response.Cookies["ItemCode"].Value = Lbl_Cookie.Text; Response.Cookies["ItemCode"].Expires = DateTime.Now.AddSeconds(5); HttpCookie aCookie = new HttpCookie("lastVisit"); aCookie.Value = DateTime.Now.ToString(); aCookie.Expires = DateTime.Now.AddMilliseconds(1); Response.Cookies["Type"].Value = Lbl_Check.Text; Response.Cookies["Type"].Expires = DateTime.Now.AddSeconds(5); Response.Cookies.Add(aCookie); LBtn_Add.Enabled = false; IBtn_Add.Enabled = false; LBtn_Modify.Enabled = true; IBtn_Modify.Enabled = true; IBtn_Delete.Enabled = true; LBtn_Delete.Enabled = true; } private void getData() { DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(); string strQuery = "SELECT Item_Code,Item_Make,Item_Product,Item_Category,Item_Name,Item_UOM,Item_Weight,Target_Pric e,Drawing_Size,Drawing_Revision_Num,Drawing_Revision_Date,Production_Type FROM Item_Master where Item_Code Like '%" + Txt_Search.Text + "%' OR Item_Make Like '%" + Txt_Search.Text + "%' OR Item_Product Like '%" + Txt_Search.Text + "%' OR Item_Category Like'%" + Txt_Search.Text + "%'"; SqlCommand cmd = new SqlCommand(strQuery); cmd.CommandType = CommandType.Text; cmd.Connection = conn; sda.SelectCommand = cmd; sda.Fill(dt); if (dt.Rows.Count < 1) {

Lbl_Error.Visible = true; Lbl_Error.Text = "No results Found"; Lbl_Error.Focus(); Pnl_Options.Enabled = false; Pnl_Options.Visible = false; } else { Pnl_Options.Enabled = true; Pnl_Options.Visible = true; } Item_Master_GridView.DataSource = dt; Item_Master_GridView.DataBind(); conn.Close(); } public void AutoAdjustColumnWidth() { List<int> len = new List<int>(); if (Item_Master_GridView.Rows.Count > 0) { int j = Item_Master_GridView.Rows[0].Cells.Count; for (int i = 0; i < j; i++) { len.Add(0); } for (int i = 0; i < Item_Master_GridView.Rows.Count; i++) { for (int cnt = 0; cnt < Item_Master_GridView.Rows[i].Cells.Count; cnt++) { if (Item_Master_GridView.Rows[i].Cells[cnt].Text.Length > len[cnt]) { len[cnt] = Item_Master_GridView.Rows[i].Cells[cnt].Text.Length; Item_Master_GridView.HeaderRow.Cells[cnt].Style.Add(HtmlTextWriterStyle.Width , (len[cnt] * 30).ToString()); Item_Master_GridView.HeaderRow.Cells[cnt].Style.Add(HtmlTextWriterStyle.Width , (len[cnt] * 30).ToString()); Item_Master_GridView.HeaderRow.Cells[cnt].Style.Add(HtmlTextWriterStyle.White Space, "nowrap"); Item_Master_GridView.HeaderRow.Cells[cnt].Wrap = false; Item_Master_GridView.Rows[i].Cells[cnt].Style.Add(HtmlTextWriterStyle.Width, (len[cnt] * 30).ToString()); Item_Master_GridView.Rows[i].Cells[cnt].Style.Add(HtmlTextWriterStyle.WhiteSpa ce, "nowrap"); Item_Master_GridView.Rows[i].Cells[cnt].Wrap = false; } } } protected void IBtn_Delete_Click(object sender, ImageClickEventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); String chkDelete1 = "Delete Item_Master Where Item_Code='" + Lbl_Cookie.Text + "'"; } }

SqlCommand cmd1 = new SqlCommand(chkDelete1, conn); conn.Open(); cmd1.ExecuteNonQuery(); conn.Close(); getData(); LBtn_Delete.OnClientClick = Lbl_Error.Text = "Delete Successful"; } protected void IBtn_Search_Click(object sender, ImageClickEventArgs e) { Lbl_Error.Visible = false; getData(); } protected void LBtn_Modify_Click(object sender, EventArgs e) { Lbl_Check.Text = "Edit"; } protected void IBtn_Modify_Click(object sender, ImageClickEventArgs e) { Lbl_Check.Text = "Edit"; } protected void LBtn_Delete_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); String chkDelete1 = "Delete Item_Master Where Item_Code='" + Lbl_Cookie.Text + "'"; SqlCommand cmd1 = new SqlCommand(chkDelete1, conn); conn.Open(); cmd1.ExecuteNonQuery(); conn.Close(); getData(); LBtn_Delete.OnClientClick = Lbl_Error.Text = "Delete Successful"; } protected void LBtn_Add_Click(object sender, EventArgs e) { Lbl_Check.Text = "Add"; Response.Cookies["Type"].Value = Lbl_Check.Text; Response.Cookies["Type"].Expires = DateTime.Now.AddSeconds(5); } }

Details Edit Page:


protected void Page_Load(object sender, EventArgs e) { Calendar1.Visible = false; if (Request.Cookies["ItemCode"] != null) { HttpCookie aCookie = Request.Cookies["ItemCode"]; HttpCookie aCookie1 = Request.Cookies["Type"]; Lbl_Cookie.Text = Server.HtmlEncode(aCookie.Value);

} else { Txt_ICode.ReadOnly = false; }

Lbl_Check.Text = Server.HtmlEncode(aCookie1.Value); Request.Cookies.Clear(); getdata(); Txt_ICode.ReadOnly = true; Btn_Changing.Text = "Save"; Lbl_Check.Text = "Edit";

} protected void getdata() { SqlConnection connection = new SqlConnection(@"Data Source=NIRMALPC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); try { SqlCommand command = new SqlCommand("Select * from Item_Master where Item_Code='" + Lbl_Cookie.Text + "'", connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); Lbl_Cookie.Text = ""; if (reader.HasRows) { while (reader.Read()) { //Assign to your textbox here Txt_ICode.Text = reader["Item_Code"].ToString(); Txt_IName.Text = reader["Item_Name"].ToString(); Txt_IWeight.Text = reader["Item_Weight"].ToString(); Txt_TPrice.Text = reader["Target_Price"].ToString(); Txt_DRevDate.Text = reader["Drawing_Revision_Date"].ToString(); DDL_DRevNo.SelectedValue = reader["Drawing_Revision_Num"].ToString(); DDL_DSize.SelectedValue = reader["Drawing_Size"].ToString(); DDL_UOM.SelectedValue = reader["Item_UOM"].ToString(); DDL_ICategory.SelectedValue = reader["Item_Category"].ToString(); DDL_IMake.SelectedValue = reader["Item_Make"].ToString(); DDL_IProd.SelectedValue = reader["Item_Product"].ToString(); DDL_ProdType.SelectedValue = reader["Production_type"].ToString(); } } catch (SqlException ex) { Response.Write(ex.Message.ToString()); } finally { connection.Close(); SqlConnection.ClearPool(connection); } }

protected void IBtn_DRevDate_Click(object sender, ImageClickEventArgs e) {

Calendar1.Visible = true; } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { Txt_DRevDate.Text = Calendar1.SelectedDate.ToString(); Calendar1.Visible = false; } protected void Btn_Changing_Click(object sender, EventArgs e) { if (Lbl_Check.Text=="Edit") { updation(); Lbl_Check.Text = ""; } else { insertion(); } } protected void updation() { try { SqlConnection connection = new SqlConnection(@"Data Source=NIRMALPC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); String chkUpdate = "Update Item_Master Set Item_Make='" + DDL_IMake.SelectedValue.ToString() + "'" + ",Item_Product='" + DDL_IProd.SelectedValue.ToString() + "',Item_Category='" + DDL_ICategory.SelectedValue.ToString() + "'" + ",Item_Name='" + Txt_IName.Text + "',Item_UOM='" + DDL_UOM.SelectedValue.ToString() + "',Item_Weight='" + Txt_IWeight.Text + "'" + ",Target_Price='" + Txt_TPrice.Text + "',Drawing_Size='" + DDL_DSize.SelectedValue.ToString() + "'" + ",Drawing_Revision_Num='" + DDL_DRevNo.SelectedValue.ToString() + "',Drawing_Revision_Date='" + Txt_DRevDate.Text + "'" + ",Production_Type='" + DDL_ProdType.SelectedValue.ToString() + "' Where Item_Code='" + Txt_ICode.Text + "'"; connection.Open(); SqlCommand cmd = new SqlCommand(chkUpdate, connection); cmd.ExecuteNonQuery(); connection.Close(); clear(); } catch (Exception ex) { Lbl_Check.Visible = true; Lbl_Check.Text="Error ... " + ex.Message; } } protected void insertion() { try { if ((Txt_DRevDate.Text != "") && (Txt_ICode.Text != "") && (Txt_IName.Text != "") && (Txt_IWeight.Text != "") && (Txt_TPrice.Text != ""))

{ try { SqlConnection connection = new SqlConnection(@"Data Source=NIRMALPC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); String chkInsert = "Insert into Item_Master(Item_Code,Item_Make,Item_Product,Item_Category,Item_Name,Item_UOM,Item_Weigh t,Target_Price,Drawing_Size,Drawing_Revision_Num,Drawing_Revision_Date,Production_Type,Item_ Current_Bal)" + "values ('" + Txt_ICode.Text + "','" + DDL_IMake.SelectedValue.ToString() + "','" + DDL_IProd.SelectedValue.ToString() + "'," + "'" + DDL_ICategory.SelectedValue.ToString() + "','" + Txt_IName.Text + "'," + "'" + DDL_UOM.SelectedValue.ToString() + "','" + Txt_IWeight.Text + "'," + "'" + Txt_TPrice.Text + "','" + DDL_DSize.SelectedValue.ToString() + "','" + DDL_DRevNo.SelectedValue.ToString() + "','" + Txt_DRevDate.Text + "','" + DDL_ProdType.SelectedValue.ToString() + "','0')"; SqlCommand cmd = new SqlCommand(chkInsert, connection); connection.Open(); cmd.ExecuteNonQuery(); connection.Close(); clear(); } catch (Exception ex) { Lbl_Check.Visible = true; Lbl_Check.Text = "Error " + ex.Message; } } else { Response.Write("Fields Cannot be empty "); } } catch (Exception ex) { Lbl_Check.Visible = true; Lbl_Check.Text = "Error " + ex.Message; } } protected void Btn_Cancel_Click(object sender, EventArgs e) { // updation(); } protected void clear() { Txt_DRevDate.Text = ""; Txt_ICode.Text = ""; Txt_IName.Text = ""; Txt_IWeight.Text = ""; Txt_TPrice.Text = ""; DDL_DRevNo.SelectedIndex = 0; DDL_DSize.SelectedIndex = 0; DDL_ICategory.SelectedIndex = 0; DDL_IMake.SelectedIndex = 0; DDL_IProd.SelectedIndex = 0; DDL_ProdType.SelectedIndex = 0; DDL_UOM.SelectedIndex = 0;

Crystal Reports :

Create Dataset , Report, Aspx Page(Crystal Report Viewer and search Options)
protected void Page_Load(object sender, EventArgs e) { data(); } private ReportDocument report = new ReportDocument(); protected void Btn_Search_Click(object sender, EventArgs e) { CrystalReportViewer1.Visible = true; data1(); } protected void data() { String search = Txt_Search.Text; SqlConnection myConnection = new SqlConnection(@"Data Source=NIRMALPC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); string selectQuery = @"SELECT * FROM Item_Master WHERE Item_Code LIKE '%" + @search + "%' OR Item_Make LIKE '%" + @search + "%' OR Item_Product LIKE '%" + @search + "%' OR Item_Name LIKE '%" + @search + "%'"; SqlCommand myCommand = new SqlCommand(selectQuery, myConnection); myCommand.Parameters.AddWithValue("@search", search); SqlDataAdapter ad = new SqlDataAdapter(myCommand); MM_Dataset Item = new MM_Dataset(); ad.Fill(Item, "Item_Master"); report.FileName = Server.MapPath("MM_Report.rpt"); report.SetDataSource(Item); CrystalReportViewer1.ReportSource = report; } protected void data1() { String search = Txt_Search.Text; SqlConnection myConnection = new SqlConnection(@"Data Source=NIRMALPC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); string selectQuery = @"SELECT * FROM Item_Master WHERE Item_Code LIKE '%" + @search + "%' OR Item_Make LIKE '%" + @search + "%' OR Item_Product LIKE '%" + @search + "%' OR Item_Name LIKE '%" + @search + "%'"; SqlCommand myCommand = new SqlCommand(selectQuery, myConnection); myCommand.Parameters.AddWithValue("@search", search); SqlDataAdapter ad = new SqlDataAdapter(myCommand); MM_Dataset Item = new MM_Dataset(); ad.Fill(Item, "Item_Master"); if (Item.Tables["Item_Master"].Rows.Count < 1) { Response.Write("<script>alert('No Existing Data');</script>"); } else { report.FileName = Server.MapPath("MM_Report.rpt"); report.SetDataSource(Item);

CrystalReportViewer1.ReportSource = report; } } }

Gridview Multiple Selection Create Temp tables : Aspx Page : <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server"> <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"> </asp:TextBox> <asp:HiddenField ID="hiddenChkCount" runat="server" Value="0" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound" DataKeyNames="Item_Code"> <Columns> <asp:TemplateField HeaderText="Selected"> <ItemTemplate><asp:CheckBox ID="chk1" runat="server"/></ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Item_Code" HeaderText="Item_Code" ReadOnly="True" SortExpression="Item_Code" /> <asp:BoundField DataField="Item_Name" HeaderText="Item_Name" SortExpression="Item_Name" /> <asp:BoundField DataField="UOM" HeaderText="UOM" SortExpression="UOM" /> <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" /> <asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" /> <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" /> <asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" /> <asp:BoundField DataField="Drawing_Rev_No" HeaderText="Drawing_Rev_No" SortExpression="Drawing_Rev_No" /> <asp:BoundField DataField="Drawing_Rev_Date" HeaderText="Drawing_Rev_Date" SortExpression="Drawing_Rev_Date" /> <asp:BoundField DataField="Drawing_Size" HeaderText="Drawing_Size" SortExpression="Drawing_Size" /> <asp:BoundField DataField="Production_Type" HeaderText="Production_Type" SortExpression="Production_Type" /> <asp:BoundField DataField="Item_Sales_Rate" HeaderText="Item_Sales_Rate" SortExpression="Item_Sales_Rate" /> </Columns> </asp:GridView> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RIMSConnectionString %>" SelectCommand="SELECT * FROM [Material_Master]"></asp:SqlDataSource> </asp:Content> 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; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Collections.Specialized;

using System.Text; namespace RIMS { public partial class WebForm4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { getdata(); } protected void getdata() { CheckBox chk; foreach (GridViewRow rowItem in GridView1.Rows) { // get reference to checkbox control placed inside the first cell of gridview control chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1")); //Label1.Text = chk.Text; // add onclick event to the checkbox to call javascript validation function chk.Attributes.Add("onclick", "chkCount(this)"); } //DataTable dt = new DataTable(); //SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMMS;Integrated Security=True"); //conn.Open(); //SqlDataAdapter sda = new SqlDataAdapter(); //string strQuery = "select * from Table_1"; //SqlCommand cmd = new SqlCommand(strQuery); //cmd.CommandType = CommandType.Text; //cmd.Connection = conn; //sda.SelectCommand = cmd; //sda.Fill(dt); //GridView1.DataSource = dt; //GridView1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { StringCollection sc = new StringCollection(); string id = string.Empty; for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows { CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chk1"); //find the CheckBox if (cb != null) { if (cb.Checked) { id = GridView1.Rows[i].Cells[1].Text; // get the id of the field to be Inserted sc.Add(id); // add the id to be Inserted in the StringCollection } }

} InsertRecords(sc); // call method for delete and pass the StringCollection values } //Multiple Inserts private void InsertRecords(StringCollection sc) { SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMS;Integrated Security=True"); StringBuilder sb = new StringBuilder(string.Empty); foreach (string item in sc) { const string sqlStatement = "INSERT INTO Table_1 (ICode) VALUES ("; sb.AppendFormat("{0}'{1}'); ",sqlStatement,item); } try { conn.Open(); SqlCommand cmd = new SqlCommand(sb.ToString(), conn); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } } }

CS Code Behind

Gridview Calculations 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; using System.Collections.Specialized; using System.Text; namespace RIMS { public partial class WebForm7 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getdata(); } protected void getdata() { DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(@"Data Source=NIRMAL-PC\SQLEXPRESS;Initial Catalog=RIMS;Integrated Security=True"); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(); string strQuery = "SELECT * FROM Temp1_Material"; SqlCommand cmd = new SqlCommand(strQuery); cmd.CommandType = CommandType.Text; cmd.Connection = conn; sda.SelectCommand = cmd; sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } protected void Txt_DiscPer_TextChanged(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows { CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chk1"); //find the CheckBox if (cb != null) {

if (cb.Checked) { var a = Int32.Parse(GridView1.Rows[i].Cells[6].Text); var d = Convert.ToInt32(Txt_Qty.Text); var b = Convert.ToInt32(Txt_DiscPer.Text); GridView1.Rows[i].Cells[8].Text = b.ToString(); GridView1.Rows[i].Cells[5].Text = d.ToString(); var c = 0; if (d >= 0 && b >= 0 && d >= 0) { a *= d; GridView1.Rows[i].Cells[7].Text = a.ToString(); c = a * b / 100; var tot1 = 0; tot1 = a - c; Lbl_DicAMnt.Text = c.ToString(); GridView1.Rows[i].Cells[9].Text = tot1.ToString(); } else { Response.Write("<script>alert('Values Must Be Non-Negative');</script>"); } } else { Response.Write("<script>alert('Select Atleast One Item');</script>"); } } } }

} }

You might also like