Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I have used the code for creating Editable Nested GridView (All-in-One)[^] for my website. But i am not able to insert values to the child gridview. Its just refreshes the page and does not show any error pages when i click add button of child grid, and the value is not inserted. Here is my page code... please help me...
 
aspx page :
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="adminpage_Default2" %>
 
<!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 id="Head1"  runat="server">
    <title>Untitled Page</title>
 
    <script language="javascript" type="text/javascript">
    function expandcollapse(obj,row)
    {
        var div = document.getElementById(obj);
        var img = document.getElementById('img' + obj);
        
        if (div.style.display == "none")
        {
            div.style.display = "block";
            if (row == 'alt')
            {
                img.src = "minus.gif";
            }
            else
            {
                img.src = "minus.gif";
            }
            img.alt = "Close to view other Customers";
        }
        else
        {
            div.style.display = "none";
            if (row == 'alt')
            {
                img.src = "plus.gif";
            }
            else
            {
                img.src = "plus.gif";
            }
            img.alt = "Expand to show Orders";
        }
    } 
    </script>
 
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:GridView ID="GridView1" AllowPaging="True" BackColor="#F1F1F1" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" DataKeyNames="ID" Style="z-index: 101; left: 8px;
            position: absolute; top: 32px" ShowFooter="True" Font-Size="Small" Font-Names="Verdana"
            runat="server" GridLines="None" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"
            OnRowUpdating="GridView1_RowUpdating" BorderStyle="Outset" OnRowDeleting="GridView1_RowDeleting"
            OnRowDeleted="GridView1_RowDeleted" OnRowUpdated="GridView1_RowUpdated" AllowSorting="True">
            <rowstyle backcolor="Gainsboro" />
            <alternatingrowstyle backcolor="White" />
            <HeaderStyle BackColor="#0083C1" ForeColor="White" />
            <footerstyle backcolor="White" />
            <columns>
                <asp:TemplateField>
                    <itemtemplate>
                        <a href="java<!-- no -->script:expandcollapse('div<%# Eval(" id=") %>', 'one');">
                            <img id="imgdiv<%# Eval(" alt="Click to show/hide Songs for the Album <%# Eval(">
                                width="9px" border="0" src="plus.gif" />
                        </img></a>
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="ID" SortExpression="id">
                    <itemtemplate>
                        <asp:Label ID="lblID" Text='<%# Eval("id") %>' runat="server">
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:Label ID="lblID" Text='<%# Eval("id") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtID" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Album Name" SortExpression="albumname">
                    <itemtemplate>
                        <%# Eval("albumname") %></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtAlbum" Text='<%# Eval("albumname") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtAlbum" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Language" SortExpression="albumlanguage">
                    <itemtemplate>
                        <%# Eval("albumlanguage") %></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtlanguage" Text='<%# Eval("albumlanguage") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtlanguage" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Music Director" SortExpression="musicdirector">
                    <itemtemplate>
                        <%# Eval("musicdirector") %></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtmusicdirector" Text='<%# Eval("musicdirector") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtmusicdirector" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Copyright" SortExpression="copyright">
                    <itemtemplate>
                        <%# Eval("copyright")%></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtcopyright" Text='<%# Eval("copyright") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtcopyright" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Year" SortExpression="year">
                    <itemtemplate>
                        <%# Eval("year")%></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtyear" Text='<%# Eval("year") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtyear" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Price" SortExpression="price">
                    <itemtemplate>
                        <%# Eval("price")%></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtprice" Text='<%# Eval("price") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtprice" Text='' runat="server">
                    </footertemplate>
                
                <asp:TemplateField HeaderText="Discount" SortExpression="discount">
                    <itemtemplate>
                        <%# Eval("discount")%></itemtemplate>
                    <edititemtemplate>
                        <asp:TextBox ID="txtdiscount" Text='<%# Eval("discount") %>' runat="server">
                    </edititemtemplate>
                    <footertemplate>
                        <asp:TextBox ID="txtdiscount" Text='' runat="server">
                    </footertemplate>
                
                <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                <asp:TemplateField HeaderText="Delete">
                    <itemtemplate>
                        <asp:LinkButton ID="linkDeleteCust" CommandName="Delete" runat="server">Delete
                    </itemtemplate>
                    <footertemplate>
                        <asp:LinkButton ID="linkAddCust" CommandName="Add" runat="server">Add
                    </footertemplate>
                
                <asp:TemplateField>
                    <itemtemplate>
                        <tr>
                            <td colspan="100%">
                                <div id="div<%# Eval(" style="display: none; position: relative; left: 15px;<br mode=" hold=" />                                    overflow: auto; width: 97%">
                                    <asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" BackColor="White"
                                        Width="100%" Font-Size="X-Small" AutoGenerateColumns="false" Font-Names="Verdana"
                                        runat="server" DataKeyNames="ID" ShowFooter="true" OnPageIndexChanging="GridView2_PageIndexChanging"
                                        OnRowUpdating="GridView2_RowUpdating" OnRowCommand="GridView2_RowCommand" OnRowEditing="GridView2_RowEditing"
                                        GridLines="None" OnRowUpdated="GridView2_RowUpdated" OnRowCancelingEdit="GridView2_CancelingEdit"
                                        OnRowDataBound="GridView2_RowDataBound" OnRowDeleting="GridView2_RowDeleting"
                                        OnRowDeleted="GridView2_RowDeleted" OnSorting="GridView2_Sorting" BorderStyle="Double"
                                        BorderColor="#0083C1">
                                        <rowstyle backcolor="Gainsboro" />
                                        <alternatingrowstyle backcolor="White" />
                                        <HeaderStyle BackColor="#0083C1" ForeColor="White" />
                                        <footerstyle backcolor="White" />
                                        <columns>
                                            <asp:TemplateField HeaderText="Song ID" SortExpression="songid">
                                                <itemtemplate>
                                                    <asp:Label ID="lblSID" Text='<%# Eval("songid") %>' runat="server">
                                                </itemtemplate>
                                                <edititemtemplate>
                                                    <asp:Label ID="lblSID" Text='<%# Eval("songid") %>' runat="server">
                                                </edititemtemplate>
                                            
                                            <asp:TemplateField HeaderText="Song Name" SortExpression="songname">
                                                <itemtemplate>
                                                    <%# Eval("songname")%></itemtemplate>
                                                <edititemtemplate>
                                                    <asp:TextBox ID="txtsongname" Text='<%# Eval("songname")%>' runat="server">
                                                </edititemtemplate>
                                                <footertemplate>
                                                    <asp:TextBox ID="txtsongname" Text='' runat="server">
                                                </footertemplate>
                                            
                                            <asp:TemplateField HeaderText="Singers" SortExpression="singers">
                                                <itemtemplate>
                                                    <%# Eval("singers")%></itemtemplate>
                                                <edititemtemplate>
                                                    <asp:TextBox ID="txtsingers" Text='<%# Eval("singers")%>' runat="server">
                                                </edititemtemplate>
                                                <footertemplate>
                                                    <asp:TextBox ID="txtsingers" Text='' runat="server">
                                                </footertemplate>
                                            
                                            <asp:TemplateField HeaderText="Time" SortExpression="time">
                                                <itemtemplate>
                                                    <%# Eval("time")%></itemtemplate>
                                                <edititemtemplate>
                                                    <asp:TextBox ID="txttime" Text='<%# Eval("time")%>' runat="server">
                                                </edititemtemplate>
                                                <footertemplate>
                                                    <asp:TextBox ID="txttime" Text='' runat="server">
                                                </footertemplate>
                                            
                                            <asp:TemplateField HeaderText="Price" SortExpression="sprice">
                                                <itemtemplate>
                                                    <%# Eval("sprice")%></itemtemplate>
                                                <edititemtemplate>
                                                    <asp:TextBox ID="txtsprice" Text='<%# Eval("sprice")%>' runat="server">
                                                </edititemtemplate>
                                                <footertemplate>
                                                    <asp:TextBox ID="txtsprice" Text='' runat="server">
                                                </footertemplate>
                                            
                                            <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                                            <asp:TemplateField HeaderText="Delete">
                                                <itemtemplate>
                                                    <asp:LinkButton ID="linkDeleteCust" CommandName="Delete" runat="server">Delete
                                                </itemtemplate>
                                                <footertemplate>
                                                    <asp:LinkButton ID="linkAddOrder" CommandName="AddSong" runat="server">Add
                                                </footertemplate>
                                            
                                        </columns>
                                    
                                </div>
                            </td>
                        </tr>
                    </itemtemplate>
                
            </columns>
        
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
            SelectCommand="SELECT [albumdetails].[id], [albumdetails].[albumname],[albumdetails].[albumlanguage],[albumdetails].[musicdirector],[albumdetails].[copyright],[albumdetails].[year],[albumdetails].[price],[albumdetails].[discount] FROM [albumdetails] ORDER BY [albumdetails].[id]"
            ProviderName="System.Data.SqlClient" runat="server">
    </div>
    </form>
</body>
</html>
 
c# page :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
 
public partial class adminpage_Default2 : System.Web.UI.Page
{
 
    #region Variables
    string gvUniqueID = String.Empty;
    int gvNewPageIndex = 0;
    int gvEditIndex = -1;
    string gvSortExpr = String.Empty;
    private string gvSortDir
    {
 
        get { return ViewState["SortDirection"] as string ?? "ASC"; }
 
        set { ViewState["SortDirection"] = value; }
 
    }
    #endregion
 
    //This procedure returns the Sort Direction
    private string GetSortDirection()
    {
        switch (gvSortDir)
        {
            case "ASC":
                gvSortDir = "DESC";
                break;
 
            case "DESC":
                gvSortDir = "ASC";
                break;
        }
        return gvSortDir;
    }
 
    //This procedure prepares the query to bind the child GridView
    private SqlDataSource ChildDataSource(string strID, string strSort)
    {
        string strQRY = "";
        SqlDataSource dsTemp = new SqlDataSource();
        dsTemp.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
        strQRY = "SELECT [songlist].[songid],[songlist].[id]," +
                                "[songlist].[albumname],[songlist].[songname],[songlist].[singers],[songlist].[time],[songlist].[sprice] FROM [songlist]" +
                                " WHERE [songlist].[id] = '" + strID + "'" +
                                "UNION ALL " +
                                "SELECT '" + strID + "','','','','','','' FROM [songlist] WHERE [songlist].[id] = '" + strID + "'" +
                                "HAVING COUNT(*)=0 " + strSort;
 
        dsTemp.SelectCommand = strQRY;
        return dsTemp;
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    #region GridView1 Event Handlers
    //This event occurs for each row
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        string strSort = string.Empty;
 
        // Make sure we aren't in header/footer rows
        if (row.DataItem == null)
        {
            return;
        }
 
        //Find Child GridView control
        GridView gv = new GridView();
        gv = (GridView)row.FindControl("GridView2");
 
        //Check if any additional conditions (Paging, Sorting, Editing, etc) to be applied on child GridView
        if (gv.UniqueID == gvUniqueID)
        {
            gv.PageIndex = gvNewPageIndex;
            gv.EditIndex = gvEditIndex;
            //Check if Sorting used
            if (gvSortExpr != string.Empty)
            {
                GetSortDirection();
                strSort = " ORDER BY " + string.Format("{0} {1}", gvSortExpr, gvSortDir);
            }
            //Expand the Child grid
            ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE="'javascript'">expandcollapse('div" + ((DataRowView)e.Row.DataItem)["id"].ToString() + "','one');</script>");
        }
 
        //Prepare the query for Child GridView by passing the Customer ID of the parent row
        gv.DataSource = ChildDataSource(((DataRowView)e.Row.DataItem)["id"].ToString(), strSort);
        gv.DataBind();
 
        //Add delete confirmation message for Customer
        LinkButton l = (LinkButton)e.Row.FindControl("linkDeleteCust");
        l.Attributes.Add("onclick", "javascript:return " +
        "confirm('Are you sure you want to delete this Album " +
        DataBinder.Eval(e.Row.DataItem, "id") + "')");
 
    }
 
    //This event occurs for any operation on the row of the grid
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Check if Add button clicked
        if (e.CommandName == "Add")
        {
            try
            {
                //Get the values stored in the text boxes
                string strID = ((TextBox)GridView1.FooterRow.FindControl("txtID")).Text;
                string stralbum = ((TextBox)GridView1.FooterRow.FindControl("txtAlbum")).Text;
                string strlanguage = ((TextBox)GridView1.FooterRow.FindControl("txtlanguage")).Text;
                string strdirector = ((TextBox)GridView1.FooterRow.FindControl("txtmusicdirector")).Text;
                string strcopyright = ((TextBox)GridView1.FooterRow.FindControl("txtcopyright")).Text;
                string stryear = ((TextBox)GridView1.FooterRow.FindControl("txtyear")).Text;
                string strprice = ((TextBox)GridView1.FooterRow.FindControl("txtprice")).Text;
                string strdiscount = ((TextBox)GridView1.FooterRow.FindControl("txtdiscount")).Text;
 
                //Prepare the Insert Command of the DataSource control
                string strSQL = "";
                strSQL = "INSERT INTO albumdetails (id,albumname,albumlanguage,musicdirector,copyright,year, " +
                        "price, discount) VALUES ('" + strID + "','" + stralbum + "','" + strlanguage + "','" + strdirector + "','" + strcopyright + "','" +
                        stryear + "','" + strprice + "','" + strdiscount + "')";
 
                SqlDataSource1.InsertCommand = strSQL;
                SqlDataSource1.Insert();
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Album added successfully');</script>");
 
                //Re bind the grid to refresh the data
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }
 
    //This event occurs on click of the Update button
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Get the values stored in the text boxes
        string strID = ((TextBox)GridView1.FooterRow.FindControl("txtID")).Text;
        string stralbum = ((TextBox)GridView1.FooterRow.FindControl("txtAlbum")).Text;
        string strlanguage = ((TextBox)GridView1.FooterRow.FindControl("txtlanguage")).Text;
        string strdirector = ((TextBox)GridView1.FooterRow.FindControl("txtmusicdirector")).Text;
        string strcopyright = ((TextBox)GridView1.FooterRow.FindControl("txtcopyright")).Text;
        string stryear = ((TextBox)GridView1.FooterRow.FindControl("txtyear")).Text;
        string strprice = ((TextBox)GridView1.FooterRow.FindControl("txtprice")).Text;
        string strdiscount = ((TextBox)GridView1.FooterRow.FindControl("txtdiscount")).Text;
 
        try
        {
            //Prepare the Update Command of the DataSource control
            string strSQL = "";
            strSQL = "UPDATE albumdetails set albumname = '" + stralbum + "'" +
                     ",albumlanguage = '" + strlanguage + "'" +
                     ",musicdirector = '" + strdirector + "'" +
                     ",copyright = '" + strcopyright + "'" +
                     ",year = '" + stryear + "'" +
                     ",price = '" + strprice + "'" +
                     ",discount = '" + strdiscount + "'" +
                     " WHERE id = '" + strID + "'";
            SqlDataSource1.UpdateCommand = strSQL;
            SqlDataSource1.Update();
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Album updated successfully');</script>");
        }
        catch { }
    }
 
    //This event occurs after RowUpdating to catch any constraints while updating
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //Check if there is any exception while deleting
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }
    }
 
    //This event occurs on click of the Delete button
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //Get the value        
        string strID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblID")).Text;
 
        //Prepare the delete Command of the DataSource control
        string strSQL = "";
 
        try
        {
            strSQL = "DELETE from albumdetails WHERE id = '" + strID + "'";
            SqlDataSource1.DeleteCommand = strSQL;
            SqlDataSource1.Delete();
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Album deleted successfully');</script>");
        }
        catch { }
    }
 
    //This event occurs after RowDeleting to catch any constraints while deleting
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //Check if there is any exception while deleting
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }
    }
    #endregion
 
    #region GridView2 Event Handlers
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvNewPageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
 
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddSong")
        {
            try
            {
                GridView gvTemp = (GridView)sender;
                gvUniqueID = gvTemp.UniqueID;
 
                //Get the values stored in the text boxes
                string strID = gvTemp.DataKeys[0].Value.ToString();  //Customer ID is stored as DataKeyNames
                string strsong = ((TextBox)gvTemp.FooterRow.FindControl("txtsongname")).Text;
                string strsingers = ((TextBox)gvTemp.FooterRow.FindControl("txtsingers")).Text;
                string strtime = ((TextBox)gvTemp.FooterRow.FindControl("txttime")).Text;
                string strsprice= ((TextBox)gvTemp.FooterRow.FindControl("txtsprice")).Text;
 
                //Prepare the Insert Command of the DataSource control
                string strSQL = "";
                strSQL = "INSERT INTO songlist (id,songname,singers,time, " +
                        "sprice) VALUES ('" + strID + "','" + strsong + "','" + strsingers + "','" +
                        strtime + "','" + strsprice + "')";
 
                SqlDataSource1.InsertCommand = strSQL;
                SqlDataSource1.Insert();
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Song added successfully');</script>");
 
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }
 
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvEditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }
 
    protected void GridView2_CancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvEditIndex = -1;
        GridView1.DataBind();
    }
 
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridView gvTemp = (GridView)sender;
            gvUniqueID = gvTemp.UniqueID;
 
            //Get the values stored in the text boxes
            string strsid = ((Label)gvTemp.Rows[e.RowIndex].FindControl("lblSID")).Text;
            string strsong = ((TextBox)gvTemp.FooterRow.FindControl("txtsongname")).Text;
            string strsingers = ((TextBox)gvTemp.FooterRow.FindControl("txtsingers")).Text;
            string strtime = ((TextBox)gvTemp.FooterRow.FindControl("txttime")).Text;
            string strsprice = ((TextBox)gvTemp.FooterRow.FindControl("txtsprice")).Text;
 
            //Prepare the Update Command of the DataSource control
            SqlDataSource dsTemp = new SqlDataSource();
            dsTemp.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
            string strSQL = "";
            strSQL = "UPDATE songlist set songname = " + strsong + "" +
                     ",singers = '" + strsingers + "'" +
                     ",time='" + strtime + "'" +
                     ",sprice = '" + strsprice + "'" +
                     " WHERE songid = " + strsid;
            dsTemp.UpdateCommand = strSQL;
            dsTemp.Update();
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Song updated successfully');</script>");
 
            //Reset Edit Index
            gvEditIndex = -1;
 
            GridView1.DataBind();
        }
        catch { }
    }
 
    protected void GridView2_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //Check if there is any exception while deleting
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }
    }
 
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
 
        //Get the value        
        string strsid = ((Label)gvTemp.Rows[e.RowIndex].FindControl("lblSID")).Text;
 
        //Prepare the Update Command of the DataSource control
        string strSQL = "";
 
        try
        {
            strSQL = "DELETE from songlist WHERE songid = " + strsid;
            SqlDataSource dsTemp = new SqlDataSource();
            dsTemp.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
            dsTemp.DeleteCommand = strSQL;
            dsTemp.Delete();
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('Order deleted successfully');</script>");
            GridView1.DataBind();
        }
        catch { }
    }
 
    protected void GridView2_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //Check if there is any exception while deleting
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE="'javascript'">alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }
    }
 
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Check if this is our Blank Row being databound, if so make the row invisible
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (((DataRowView)e.Row.DataItem)["songid"].ToString() == String.Empty) e.Row.Visible = false;
        }
    }
 
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvSortExpr = e.SortExpression;
        GridView1.DataBind();
    }
    #endregion
 
}
Posted 8-Jan-11 7:08am
Comments
OriginalGriff at 9-Jan-11 4:24am
   
"sorry for this... i will do the needful... thank u... - sharathu7 3 hrs ago"
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't post this under Questions & Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 

Trust me, nobody here is going to read that much code and work out what your problem is!
When you post code, cut it down to the relevant bits - would you like to wade through that lot?
  Permalink  
Comments
sharathu7 at 9-Jan-11 0:32am
   
sorry for this... i will do the needful... thank u...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

set the identity increment 1 in sql server
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100