Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGridView();
        }
    }

    private void FillGridView()
    {
        try
        {
            string cnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
            SqlConnection con = new SqlConnection(cnString);
            GlobalClass.adap = new SqlDataAdapter("select * from TechnisionStatus", con);
            SqlCommandBuilder bui = new SqlCommandBuilder(GlobalClass.adap);
            GlobalClass.dt = new DataTable();
            GlobalClass.adap.Fill(GlobalClass.dt);
            GridView1.DataSource = GlobalClass.dt;
            GridView1.DataBind();
        }
        catch
        {
            Response.Write("<script>alert('ConnectionString Error...')</script>");
        }
    }
        protected void editRecord(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        FillGridView();
    }
        protected void cancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillGridView();

        }
        protected void AddNewRecord(object sender, EventArgs e)
        {
            
            try
            {
                if (GlobalClass.dt.Rows.Count > 0)
                {
                    GridView1.EditIndex = -1;
                    GridView1.ShowFooter = true;
                    FillGridView();
                }
                else
                {
                    GridView1.ShowFooter = false;
                    DataRow dr = GlobalClass.dt.NewRow();
                    dr["Date"] = 0;
                    dr["TechnisionName"] = 0;
                    dr["Task"] = 0;
                    dr["Status"] = 0;
                    dr["Remark"] = 0;
                    GlobalClass.dt.Rows.Add(dr);
                    GridView1.DataSource = GlobalClass.dt;
                    GridView1.DataBind();
                    GridView1.Rows[0].Visible = true;
                }
            }
            catch
            {
                Response.Write("<script> alert('Row not added in DataTable...') </script>");
            }
        }
        protected void AddNewCancel(object sender, EventArgs e)
        {
            GridView1.ShowFooter = false;
            FillGridView();
        }
    protected void InsertNewRecord(object sender, EventArgs e)
    {
        try
        {
            string strName = GlobalClass.dt.Rows[0]["TechnisionName"].ToString();
            if (strName == "0")
            {
                GlobalClass.dt.Rows[0].Delete();
                GlobalClass.adap.Update(GlobalClass.dt);
            }
            TextBox txtDate = GridView1.FooterRow.FindControl("txtNewDate") as TextBox;
            TextBox txtName = GridView1.FooterRow.FindControl("txtNewName") as TextBox;
            TextBox txtTask = GridView1.FooterRow.FindControl("txtNewTask") as TextBox;
            TextBox txtStatus = GridView1.FooterRow.FindControl("txtNewStatus") as TextBox;
            TextBox txtRemark = GridView1.FooterRow.FindControl("txtNewRemark") as TextBox;
            DataRow dr = GlobalClass.dt.NewRow();
            dr["Date"] = txtDate.Text.Trim();
            dr["TechnisionName"] = txtName.Text.Trim();
            dr["Task"] = txtTask.Text.Trim();
            dr["Status"] = txtStatus.Text.Trim();
            dr["Remark"] = txtRemark.Text.Trim();
            GlobalClass.dt.Rows.Add(dr);
            GlobalClass.adap.Update(GlobalClass.dt);
            GridView1.ShowFooter = false;
            FillGridView();
            }
        catch
        {
             Response.Write("<script> alert('Record not added...') </script>");
        }
    }
     
    
}
Posted
Updated 21-Jan-14 2:49am
v3
Comments
[no name] 21-Jan-14 8:49am    
Have you tried debugging it with a break point.
ashwini dharamsale 21-Jan-14 8:54am    
s i tried but still it is not storing
ashwini dharamsale 21-Jan-14 9:00am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True"

AutoGenerateColumns="False"

OnRowCancelingEdit="cancelRecord" OnRowEditing="editRecord"

CellPadding="3" GridLines="Horizontal" Width="1297px" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" >

<rowstyle horizontalalign="Center" backcolor="#E7E7FF" forecolor="#4A3C8C">

<alternatingrowstyle backcolor="#F7F7F7">

<footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C">

<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />

<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />

<rowstyle backcolor="#E3EAEB">

<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />



<columns>



<asp:TemplateField>

<HeaderTemplate>Date</HeaderTemplate>

<itemtemplate>

<asp:Label ID ="lblDate" runat="server" Text='<%#Bind("Date")%>'>


<edititemtemplate>

<asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date") %>' MaxLength="50">

<asp:RequiredFieldValidator ID="rfvtxtDate" runat="server" Text="*" ToolTip="Enter Date" ControlToValidate="txtDate">

<asp:RegularExpressionValidator ID="revtxtDate" runat="server" Text="*" ToolTip="Enter Date " ControlToValidate="txtDate" ValidationExpression="^[a-zA-Z'.\s]{1,40}$">





<footertemplate>

<asp:TextBox ID="txtNewDate" runat="server" MaxLength="50">

<asp:RequiredFieldValidator ID="rfvtxtNewDate" runat="server" Text="*" ToolTip="Enter Date" ControlToValidate="txtNewDate">

<asp:RegularExpressionValidator ID="revtxtNewDate" runat="server" Text="*" ToolTip="Enter Date " ControlToValidate="txtNewDate" ValidationExpression="^[a-zA-Z'.\s]{1,40}$">









<asp:TemplateField>

<HeaderTemplate>TechnisionName</HeaderTemplate>

<itemtemplate>

<asp:Label ID ="lblName" runat="server" Text='<%#Bind("TechnisionName") %>'>



<edititemtemplate>

<asp:TextBox ID="txtName" runat="server" Text='<%#Bind("TechnisionName") %>' MaxLength="50">

<asp:RequiredFieldValidator ID="rfvtxtName" runat="server" Text="*" ToolTip="Enter name" ControlToValidate="txtName">

<asp:RegularExpressionValidator ID="revtxtName" runat="server" Text="*" ToolTip="Enter alphabate " ControlToValidate="txtName" ValidationExpression="^[a-zA-Z'.\s]{1,40}$">





<footertemplate>

<asp:TextBox ID="txtNewName" runat="server" MaxLength="50">

<asp:RequiredFieldValidator ID="rfvtxtNewName" runat="server" Text="*" ToolTip="Enter name" ControlToValidate="txtNewName">

<asp:RegularExpressionValidator ID="revtxtNewName" runat="server" Text="*" ToolTip="Enter alphabate " ControlToValidate="txtNewName" ValidationExpression="^[a-zA-Z'.\s]{1,40}$">
ashwini dharamsale 21-Jan-14 9:01am    
Here it self iam not able to pass while debuging

protected void InsertNewRecord(object sender, EventArgs e)
{
try
{
string strName = GlobalClass.dt.Rows[0]["TechnisionName"].ToString();
if (strName == "0")
{
GlobalClass.dt.Rows[0].Delete();
GlobalClass.adap.Update(GlobalClass.dt);
}
TextBox txtDate = GridView1.FooterRow.FindControl("txtNewDate") as TextBox;
TextBox txtName = GridView1.FooterRow.FindControl("txtNewName") as TextBox;
TextBox txtTask = GridView1.FooterRow.FindControl("txtNewTask") as TextBox;
TextBox txtStatus = GridView1.FooterRow.FindControl("txtNewStatus") as TextBox;
TextBox txtRemark = GridView1.FooterRow.FindControl("txtNewRemark") as TextBox;
DataRow dr = GlobalClass.dt.NewRow();
dr["Date"] = txtDate.Text.Trim();
dr["TechnisionName"] = txtName.Text.Trim();
dr["Task"] = txtTask.Text.Trim();
dr["Status"] = txtStatus.Text.Trim();
dr["Remark"] = txtRemark.Text.Trim();
GlobalClass.dt.Rows.Add(dr);
GlobalClass.adap.Update(GlobalClass.dt);
GridView1.ShowFooter = false;
FillGridView();
}
catch
{
Response.Write("<script> alert('Record not added...') </script>");
}
tgrt 21-Jan-14 13:46pm    
What are the effects of it not working specifically? Are you getting the alert saying Record not added? If so, that means your catch block is kicking in. It's not written well. It's catching everything (something you should rarely do) and it's tossing away your exception information.

Change the catch block to accept an exception and then write that out so you can see what's happening. Use ToString so you get all of it including the stack trace.

1 solution

Are you trying to insert and show data only on the grid
OR
you also want to update the newly inserted data in the database???

In second case you need to include SQL INSERT statement which is missing on your code..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900