Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi. i want Add one item from GridGroup to GridProf Group but when I click btnAdd don't happen any thing. in my page i have these control:

It shows courses in dropdownList and when I select an item from dpdCourse it loads course's groups in GridGroup:
C#
<asp:SqlDataSource ID="SqlCourse" runat="server" 
        ConnectionString="<%$ ConnectionStrings:HomeWorkSystemConnectionString %>" 
        SelectCommand="SELECT [Name], [Id] FROM [Course]"></asp:SqlDataSource>

     <asp:DropDownList ID="dpdCourse" CssClass= "txt" runat="server" 
               DataSourceID="SqlCourse" DataTextField="Name" DataValueField="Id" 
               AppendDataBoundItems="True" AutoPostBack="True">
               <asp:ListItem Text="select" Value="0"></asp:ListItem>
           </asp:DropDownList>

  <asp:SqlDataSource ID="SqlGroup" runat="server" 
        ConnectionString="<%$ ConnectionStrings:HomeWorkSystemConnectionString %>" 
        SelectCommand="SELECT [code], [Day], [Hour], [Capacity], [NumberOfStd], [Id] FROM [CourseGroup] WHERE ([CursId] = @CursId)">
        <SelectParameters>
            <asp:ControlParameter ControlID="dpdCourse" Name="CursId" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

 <asp:GridView ID="GridCrsGroup" runat="server" AutoGenerateColumns="False" 
        CssClass ="GridStyle" DataKeyNames="Id" DataSourceID="SqlGroup">
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#99CCFF" ForeColor="#333333" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:ButtonField ButtonType="Button" CommandName="Select" DataTextField="Id" 
                Text="Button">
                <ControlStyle BackColor="#006699" />
            </asp:ButtonField>
            <asp:BoundField DataField="code" HeaderText="code" SortExpression="code" />
            <asp:BoundField DataField="Day" HeaderText="Day" SortExpression="Day" />
            <asp:BoundField DataField="Hour" HeaderText="Hour" SortExpression="Hour" />
            <asp:BoundField DataField="Capacity" HeaderText="Capacity" 
                SortExpression="Capacity" />
            <asp:BoundField DataField="NumberOfStd" HeaderText="NumberOfStd" 
                SortExpression="NumberOfStd" />
        </Columns>
        
    </asp:GridView>


And i have one dropdownList to show Professor when select an item it shows professor's group in GridProfGroup:

C#
<asp:SqlDataSource ID="SqlProf" runat="server"
       ConnectionString="<%$ ConnectionStrings:HomeWorkSystemConnectionString %>"
       SelectCommand="SELECT [Id], [Family] FROM [User] WHERE ([UserType] = @UserType)">
       <SelectParameters>
           <asp:Parameter DefaultValue="P" Name="UserType" Type="String" />
       </SelectParameters>
   </asp:SqlDataSource>

 <asp:DropDownList ID="dpdProf" CssClass ="txt" runat="server"
              AppendDataBoundItems="True" DataSourceID="SqlProf" DataTextField="Family"
              DataValueField="Id" AutoPostBack="True">
              <asp:ListItem Text ="انتخاب کنید" Value= "0"></asp:ListItem>
          </asp:DropDownList>

<asp:SqlDataSource ID="SqlProfGroup" runat="server"
       ConnectionString="<%$ ConnectionStrings:HomeWorkSystemConnectionString %>"
       SelectCommand="SELECT [code], [Day], [Hour], [Capacity], [NumberOfStd], [Id] FROM [CourseGroup] WHERE ([PrfId] = @PrfId)">
       <SelectParameters>
           <asp:ControlParameter ControlID="dpdProf" Name="PrfId"
               PropertyName="SelectedValue" Type="Int32" />
       </SelectParameters>
   </asp:SqlDataSource>

<asp:GridView ID="GridProfGroup" runat="server" CssClass ="GridStyle"
       AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlProfGroup">
       <Columns>
           <asp:ButtonField ButtonType="Button" CommandName="Select" DataTextField="Id"
               Text="Button">
               <ControlStyle BackColor="#006699" />
           </asp:ButtonField>
           <asp:BoundField DataField="code" HeaderText="کد" SortExpression="code" />
           <asp:BoundField DataField="Day" HeaderText="روز" SortExpression="Day" />
           <asp:BoundField DataField="Hour" HeaderText="ساعت" SortExpression="Hour" />
           <asp:BoundField DataField="Capacity" HeaderText="ظرفیت"
               SortExpression="Capacity" />
           <asp:BoundField DataField="NumberOfStd" HeaderText="تعداد دانشجویان"
               SortExpression="NumberOfStd" />
       </Columns>


and I have a button that add selected item from GridGroup to GridProfGroup and add it to professor's list<group>:

In code behind:
C#
protected void btnAdd_Click(object sender, EventArgs e)
  {
      int index = (int)GridCrsGroup.SelectedDataKey.Value;
      var group = DB.CourseGroups.Where(g => g.Id == index).First();
      int profId = Convert.ToInt32( dpdProf.SelectedItem.Value);
      Professor selectedProf = (Professor)DB.Users.Where(p => p.Id == profId).First();
      selectedProf.AddGroup(group);
      group.AssignProfessor(selectedProf);
      GridProfGroup.DataBind();

  }


And in CourseGroup tabel DB ProfId(professor's Id) is still null. If it helps I Change the ProfId to : Allow null = Yes, because where I were defining a group I were not assign its professor I assign it in this Page.

thank you.
Posted
Updated 2-Jun-12 20:07pm
v6
Comments
taha bahraminezhad Jooneghani 1-Jun-12 5:17am    
give me your database create script, and i will help to find out were is the problem!
parisa heidari 1-Jun-12 14:55pm    
Excuse me but I'm very new in programing which file is database create script? And how can I give it to you? Do I copy it like above?
taha bahraminezhad Jooneghani 1-Jun-12 15:20pm    
go to my profile, there is a homepage link, go to my website, from contact , get my mail, and mail it to me!
in sql manager right click in your database, script as , create to, sql file and mail it to me!
Rajesh Kariyavula 3-Jun-12 3:51am    
In your DBML check if there is a foreign key relationship between the CourseGroup and Professor.

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