Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m providing with u the code it is a running code the only problem that i m unable to find out it why the gridview in not updating even though the query is correct. please help me on this.

in aspx.cs file

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Cascadingmenu
{
    public partial class _Default : System.Web.UI.Page
    {
        override protected void OnInit(EventArgs e)
        {
            box();
            base.OnInit(e);
            int count = Session.Count;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            if (!IsPostBack)
            {
                tech();
                bind();
                ddlspec.SelectedIndex = 0;
            }

        }
    

        protected void Button1_Click(object sender, EventArgs e)
        {
             int chkSpec = 0;
            string yearOfExp = " ";
            string lastUsed = "";
            int k = Convert.ToInt32(Session["fill"]);
            int i = 0;
            for (; i < k; i++)
            {

                //CheckBox chk = new CheckBox();
                CheckBox chk = (CheckBox)Panel2.FindControl("ch1" + i);
                if (chk.Checked == true)
                {
                    chkSpec = 1;

                }
                else
                {
                    chkSpec = 0;
                    // cmd1.Parameters.Add("@ch1", SqlDbType.Bit).Value = 0;
                }

                TextBox txtYrOfExp = (TextBox)Panel2.FindControl("txt1" + i);
                yearOfExp = txtYrOfExp.Text;
                TextBox txtLstUsd = (TextBox)Panel2.FindControl("txt2" + i);
                lastUsed = txtLstUsd.Text;

                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";

                con.Open();

                string query1 = "insert into detail1(text1,text2,text3,subjects,text5,text6,text7,text8)values('" + chkSpec + "','" + yearOfExp + "','" + lastUsed + "','" + ddltech.SelectedItem.Text + "','" + ddlspec.SelectedItem.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "')";
                SqlCommand cmd1 = new SqlCommand(query1, con);
                int numrow1 = cmd1.ExecuteNonQuery();
                con.Close();
            }

            Response.Redirect("default.aspx");   
        }

    
     private void autofill(int specval)
        {

            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select skill,techval from level where specval=@specval";
            cmd.Parameters.AddWithValue("@specval", specval);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(ds);
            Session["ds"] = ds;
            con.Close();
            int i = ds.Tables[0].Rows.Count;
            Session["fill"] = i;
            box();
        }
        private void tech()
        {
            SqlConnection con = new SqlConnection();
            //con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Desktop\WebApplication7\WebApplication7\WebApplication7\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select techval,subjects from tech";
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddltech.DataSource = ds.Tables[0];
                ddltech.DataTextField = "subjects";
                ddltech.DataValueField = "techval";
                ddltech.DataBind();
                ddltech.Items.Insert(0, "--choose--");

            }
            else
            {
                Label1.Text = "choose appropriate value";
                Label1.Visible = true;
            }
        }
        private void specs(int techval)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select specs , specval  from specs where techval=@techval";
            cmd.Parameters.AddWithValue("@techval", techval);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(ds);
            
            ddlspec.DataSource = ds;
            ddlspec.DataTextField = "specs";
            ddlspec.DataValueField = "specval";
            ddlspec.DataBind();
            ddlspec.Items.Insert(0, "--Select--");
            con.Close();
        }
         public void box()
         {
             int i = 0;
             int k = Panel2.Controls.Count;
             DataSet ds1 = new DataSet();
             if (Session["ds"] != null)
             {
                 ds1 = (DataSet)Session["ds"];
                 if (Session["fill"] != null)
                 {
                     i = Convert.ToInt32(Session["fill"].ToString());
                 }
                 for (int j = 0; j < i; j++)
                 {
                     TableRow skillTable = new TableRow();
                     TableCell cell1 = new TableCell();
                     TableCell cell2 = new TableCell();
                     TableCell cell3 = new TableCell();
                     TableCell cell4 = new TableCell();
                     TableCell cell5 = new TableCell();
                     TableCell cell6 = new TableCell();
                     cell1.Text = ds1.Tables[0].Rows[j][0].ToString();
                     CheckBox ch1 = new CheckBox();
                     ch1.ID = "ch1" + j.ToString();
                     cell2.Controls.Add(ch1);
                     cell3.Text = "Year Of Exp";
                     TextBox txt1 = new TextBox();
                     txt1.ID = "txt1" + j.ToString();
                     cell4.Controls.Add(txt1);
                     cell5.Text = "Recently Used ";
                     TextBox txt2 = new TextBox();
                     txt2.ID = "txt2" + j.ToString();
                     cell6.Controls.Add(txt2);
                     skillTable.Cells.Add(cell1);
                     skillTable.Cells.Add(cell2);
                     skillTable.Cells.Add(cell3);
                     skillTable.Cells.Add(cell4);
                     skillTable.Cells.Add(cell5);
                     skillTable.Cells.Add(cell6);
                     Table1.Rows.Add(skillTable);
                 }
             }
         }


        protected void ddltech_SelectedIndexChanged(object sender, EventArgs e)
        {
            int techval = Convert.ToInt32(ddltech.SelectedValue.ToString());
            specs(techval);
            Panel2.Controls.Clear();
            
           

        }

        protected void ddlspec_SelectedIndexChanged(object sender, EventArgs e)
        {
            int specs = Convert.ToInt32(ddlspec.SelectedValue.ToString());           
            autofill(specs);
            Panel2.Visible = true;
            ddlspec.SelectedIndex = 0;
            
            
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void bind()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            con.Open();
            SqlCommand cmd;

            string query = "Select * from detail1";
            cmd = new SqlCommand(query, con);
            SqlDataAdapter sda = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            con.Close();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //int id = Convert.ToInt32(e.CommandArgument);
            //if (e.CommandName == "Delete")
            //{

               


            //    SqlConnection con = new SqlConnection();
            //    con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\documents\visual studio 2010\Projects\WebApplication7\WebApplication7\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            //    con.Open();
            //    string query = "delete from detail1 where id=" + id.ToString();
            //    SqlCommand cmd = new SqlCommand(query, con);
            //    int numrow = cmd.ExecuteNonQuery();
            //    con.Close();

            //}
        }
                  

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i;
            GridView1.EditIndex = 0;
            int rowindex = GridView1.EditIndex;
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            //GridView1.EditIndex = e.NewEditIndex;
            string[] cellvalue = new string[10];
            for (i = 0; i < 8; i++)
            {
                DataControlFieldCell cell = (DataControlFieldCell)GridView1.Rows[rowindex].Cells[i];
                BoundField field = (BoundField)cell.ContainingField;
                System.Collections.Specialized.OrderedDictionary dict = new System.Collections.Specialized.OrderedDictionary();
                field.ExtractValuesFromCell(dict, cell, DataControlRowState.Edit, true);
                cellvalue[i] = dict[cell.ContainingField.ToString()].ToString();
            }
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\documents\visual studio 2010\Projects\WebApplication7\WebApplication7\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            con.Open();
            string query = "update detail1 set text1='" + cellvalue[0] + "',text2='" + cellvalue[1] + "',text3='" + cellvalue[2] + "',subjects='" + cellvalue[3] + "',text5='" + cellvalue[4] + "',text6='" + cellvalue[5] + "',text7='" + cellvalue[6] + "',text8='" + cellvalue[7] + "' where id=" + id.ToString(); 
            SqlCommand cmd = new SqlCommand(query, con);
            int numrow = cmd.ExecuteNonQuery();
            con.Close();
            con.Dispose();
            bind();
           // Response.Redirect("default.aspx");
           
            

        }        

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{

            //    LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            //    LinkButton l1 = (LinkButton)e.Row.FindControl("LinkButton2");
            //    //l1.Attributes.Add("onclick", "javascript:return " + "confirm('You are going to update your records" + DataBinder.Eval(e.Row.DataItem, "id") + "')");
            //    //l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record " + DataBinder.Eval(e.Row.DataItem, "id") + "')");

            //}

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
           
            int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            //GridView1.EditIndex = e.RowIndex + 1;
            //int i = GridView1.EditIndex;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ayush_pratap_singh\Documents\Visual Studio 2010\Projects\Cascadingmenu\Cascadingmenu\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            con.Open();
            string query = "delete from detail1 where id=" + i.ToString();
            SqlCommand cmd = new SqlCommand(query, con);
            int numrow = cmd.ExecuteNonQuery();
            con.Close();
            cmd.Dispose();
            con.Dispose();
            bind();
            Response.Redirect("default.aspx");


        }

       

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();

        }

        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            if (e.Exception == null)
            {
               Label1.Text = "Row updated successfully.";
            }
            else
            {
                e.ExceptionHandled = true;
               Label1.Text = "An error occurred while attempting to update the row.";
            }
            
            bind();
            //Response.Redirect("default.aspx");
        }


    }
}

in the aspx file.
<asp:content>
XML
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <p>
        TECHNOLOGY<asp:DropDownList ID="ddltech" runat="server" Height="26px"
            onselectedindexchanged="ddltech_SelectedIndexChanged" Width="146px" AutoPostBack="true">
        </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        SKILLS<asp:DropDownList ID="ddlspec" runat="server"
            onselectedindexchanged="ddlspec_SelectedIndexChanged" Height="27px"
            Width="174px" AutoPostBack="true">
        </asp:DropDownList>
    </p>
    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    <br />
    <br />
    SKILL:-<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TECHNOLOGY:-<asp:TextBox ID="TextBox8"
        runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp; EXPERIENCE:-<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
    <br />
<br />
    <br />
    <asp:Panel ID="Panel2" runat="server" Height="142px" Visible="False">
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
    </asp:Panel>
    <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="id" CellPadding="4"
        ForeColor="#333333" GridLines="None" Height="104px" Width="468px"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True"
        ondatabound="GridView1_DataBound"
        onpageindexchanging="GridView1_PageIndexChanging"
        onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound"
        onrowdeleting="GridView1_RowDeleting" onrowupdating="GridView1_RowUpdating"
        onselectedindexchanged="GridView1_SelectedIndexChanged" PageSize="5"
        onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="text1" HeaderText="text1" ReadOnly="true"  SortExpression="text1" />
            <asp:BoundField DataField="text2" HeaderText="text2" SortExpression="text2" />
            <asp:BoundField DataField="text3" HeaderText="text3" SortExpression="text3" />
            <asp:BoundField DataField="subjects"  HeaderText="subjects" ReadOnly="true"
                SortExpression="subjects" />
            <asp:BoundField DataField="text5" HeaderText="text5" SortExpression="text5" />
            <asp:BoundField DataField="text6" HeaderText="text6" SortExpression="text6" />
            <asp:BoundField DataField="text7" HeaderText="text7" SortExpression="text7" />
            <asp:BoundField DataField="text8" HeaderText="text8" SortExpression="text8" />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                ReadOnly="true" SortExpression="id" />

               <asp:TemplateField>

        <EditItemTemplate>
            <asp:LinkButton ID="LinkButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" CommandArgument='<%# Eval("id") %>'></asp:LinkButton>
            <asp:LinkButton ID="LinkButtonCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>

        <ItemTemplate>
            <asp:LinkButton ID="LinkButtonEdit" runat="server" CausesValidation="True" CommandName="Edit" Text="Edit" CommandArgument='<%# Eval("id") %>'></asp:LinkButton>
            <asp:LinkButton ID="LinkButtonDelete" runat="server"  CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("id") %>'> </asp:LinkButton>
        </ItemTemplate>

       <%-- <FooterTemplate>
            <asp:Button ID="Add" runat="server" CausesValidation="false" CommandName="AddNew" Text="Insert"></asp:LinkButton>
            <asp:Button ID="Cancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </FooterTemplate>--%>
        </asp:TemplateField>


        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
     SelectCommand="SELECT * FROM [detail1]"></asp:SqlDataSource>
Posted
Updated 1-Apr-13 20:36pm
v2
Comments
Sandeep Mewara 2-Apr-13 10:12am    
What do you see when you debug?
singhayush 4-Apr-13 1:37am    
its a running code the only problem that i m facing is that it does not update the database on after filling the values..

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