Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here I created a grideview containging joblist.below that i added one datadetailsview .when anybody select the row in gridview, it will shown in detail view...I added one column checkbox in the detailview.when anbody select the checkbox and click apply button below ,the data in the detailview must be added to another table applied jobs.how can we do that?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class joblist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName"), new DataColumn("Designation") });
            dt.Rows.Add("NIIT", "programmer");
            dt.Rows.Add("annvision", "Webdesigner");
            dt.Rows.Add("softtech", "designer");
            dt.Rows.Add("Mahad", "Programmer");
            DetailsView1.DataSource = dt;
            DetailsView1.DataBind();
        }
  
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/secure.aspx");
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (GridView1.SelectedRow == null)
        {
            DetailsView1.Visible = false;
        }
        else
        {
            DetailsView1.Visible = true;
        }
    }
    
    protected void Apply_Click(object sender, EventArgs e)
    {
        Response.Write("You are successfully applied");
        
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName"), new DataColumn("Designation") });
        foreach (DetailsViewRow row in DetailsView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                if (chkRow.Checked)
                {
                    string CompanyName = row.Cells[1].Text;
                    string Designation = (row.Cells[2].FindControl("lblDesignation") as Label).Text;
                    dt.Rows.Add(CompanyName, Designation);
                }
            }
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();
 
    }
 

}

it showing error near detailview1.DataBind();
error:Invalid OperationException was unhandled by usercode
Both DataSource and DataSourceID are defined on 'DetailsView1'. Remove one definition.
Posted
Updated 28-Dec-13 21:17pm
v3
Comments
JoCodes 29-Dec-13 3:16am    
Dont repost the same questiion . Instead try to update the question .
your previous question which was the same , http://www.codeproject.com/Questions/703022/add-checkbox-to-a-exsisting-gridview
Have you defined DataSourceID somewhere in .cs or aspx page?
Member 10467514 29-Dec-13 3:28am    
no
Member 10467514 29-Dec-13 3:32am    
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="joblist.aspx.cs" Inherits="joblist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
    <asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="JobID" DataSourceID="SqlDataSource1"
Width="426px" ForeColor="#333333" GridLines="None">
<alternatingrowstyle backcolor="White" forecolor="#284775">
<columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="JobID" HeaderText="JobID"
SortExpression="JobID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Designation" HeaderText="Designation"
SortExpression="Designation" />

<editrowstyle backcolor="#999999">
<footerstyle backcolor="#5D7B9D" forecolor="White" font-bold="True">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<rowstyle forecolor="#333333" backcolor="#F7F6F3">
<SelectedRowStyle BackColor="#E2DED6" ForeColor="#333333" Font-Bold="True" />
<sortedascendingcellstyle backcolor="#E9E7E2">
<sortedascendingheaderstyle backcolor="#506C8C">
<sorteddescendingcellstyle backcolor="#FFFDF8">
<sorteddescendingheaderstyle backcolor="#6F8DAE">


</p>
<p>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="2" DataKeyNames="JobID" DataSourceID="SqlDataSource2"
ForeColor="Black" GridLines="None" Height="50px" Width="300px"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px">
<alternatingrowstyle backcolor="PaleGoldenrod">
<editrowstyle backcolor="DarkSlateBlue" forecolor="GhostWhite">
<fields>
<asp:BoundField DataField="JobID" HeaderText="JobID" InsertVisible="False"
ReadOnly="True" SortExpression="JobID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="Designation" HeaderText="Designation"
SortExpression="Designation" />
<asp:BoundField DataField="EducationalQualification"
HeaderText="EducationalQualification"
SortExpression="EducationalQualification" />
<asp:BoundField DataField="Experience" HeaderText="Experience"
SortExpression="Experience" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />

<asp:TemplateField HeaderText="Select here to apply for this job">
<itemtemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />




<footerstyle backcolor="Tan">
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlate

1 solution

Quote:
error:Invalid OperationException was unhandled by usercode
Both DataSource and DataSourceID are defined on 'DetailsView1'. Remove one definition.
The Exception is quite clear.

If you look at your code, it becomes more clear.

In aspx page...
XML
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    CellPadding="2" DataKeyNames="JobID" DataSourceID="SqlDataSource2"
    ForeColor="Black" GridLines="None" Height="50px" Width="300px"
       BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px">

In .aspx.cs page...
C#
protected void Page_Load(object sender, EventArgs e)
    {
       if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName"), new DataColumn("Designation") });
            dt.Rows.Add("NIIT", "programmer");
            dt.Rows.Add("annvision", "Webdesigner");
            dt.Rows.Add("softtech", "designer");
            dt.Rows.Add("Mahad", "Programmer");
            DetailsView1.DataSource = dt;
            DetailsView1.DataBind();
        }

    }


You have to delete one as it is conflicting and can't decide which DataSource to assign to DetailsView.
 
Share this answer
 
Comments
Member 10467514 29-Dec-13 8:09am    
can't we give its ID.?if it works.........
I can't understand you. You have to provide one of DataSource or DataSourceID.
If you provide both, then it will give you error.

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