how to delete values in case of foreign key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagdeep gupta
    New Member
    • Aug 2010
    • 98

    how to delete values in case of foreign key

    How to delete values in case of foreign key?

    Please check out the code:
    Code:
    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MonthlyFeeDetail.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <table id="TABLE1" onclick="return TABLE1_onclick()">
      
           <tr> 
           <td style="height: 396px; background-color:burlywood; width: 508px; font-size: large; color: #990000;">
               <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
                   DataFile="~/App_Data/college.mdb" SelectCommand="SELECT * FROM [Year]"></asp:AccessDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames ="Month"
                   DataSourceID="AccessDataSource1" Font-Size="Large"
                    BorderColor="Silver" CellSpacing="2" 
                   AutoGenerateSelectButton="True">
            <Columns>
                
                <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
                <asp:BoundField DataField="Month" HeaderText="Month" SortExpression="Month" />
            </Columns>
            <HeaderStyle BackColor="Gray" />
        </asp:GridView>
        </td>
        <td style="height: 396px;  background-color: burlywood; width: 1397px; font-size: large; color: #990000;">
            <span style="font-size: 16pt; color: #006699">Fees Record of Students</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
                DataFile="~/App_Data/college.mdb" 
                SelectCommand="SELECT * FROM [MonthlyFee] WHERE ([Month] = ?)" UpdateCommand ="UPDATE MonthlyFee set Month=@Month,RollNo=@RollNo,StuName=@StuName, Class=@Class,SubmitFee=@SubmitFee,RecieptNo=@RecieptNo where Month=@Month"
                 DeleteCommand="Delete From MonthlyFee where Class=@Class AND RollNo=@RollNo">
                <SelectParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="Month" 
                        PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:AccessDataSource>
            <asp:GridView ID="GridView2" runat="server" 
                AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" AutoGenerateSelectButton ="true"   
                DataSourceID="AccessDataSource2" Font-Size="Large" CellSpacing="2" 
                Height="198px" Width="941px" AllowPaging="True" 
                AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="Month" HeaderText="Month" SortExpression="Month" />
                    <asp:BoundField DataField="RollNo" HeaderText="RollNo" 
                        SortExpression="RollNo" />
                    <asp:BoundField DataField="StuName" HeaderText="StuName" 
                        SortExpression="StuName" />
                    <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
                    
                    <asp:BoundField DataField="RecieptNo" HeaderText="RecieptNo" 
                        SortExpression="RecieptNo" />
                </Columns>
                <HeaderStyle BackColor="Gray" BorderColor="Gray" />
            </asp:GridView>
        </td>
        <td style="width: 7368px; height: 396px;">
            &nbsp;
        </td>
        </tr>
        <tr>
       
         <td style="font-size: large; text-align: left; height: 228px; background-color:Silver; width: 508px; color: #990000;" 
                bgcolor="#FF3300">
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" Text="Add New Records" style="color: #cc0000"></asp:Label>
            <br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="RollNo"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label3" runat="server" Text="Month"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label4" runat="server" Text="StuName"></asp:Label>&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:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label5" runat="server" Text="Class"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label6" runat="server" Text="SubmitFee"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
            <br />
            <asp:Label ID="Label7" runat="server" Text="RecieptNo"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
            <br />
             <asp:Button ID="Button1" runat="server" Text="AddNew" onclick="Button1_Click" />
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <br />
             
       
        </td>
        </tr>
    </table>
    
    </asp:Content>
    Last edited by Frinavale; Sep 7 '10, 04:12 PM. Reason: Added the question to the thread.
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    please elaborate your query.......... .what do you want...??

    Comment

    • jagdeep gupta
      New Member
      • Aug 2010
      • 98

      #3
      Like given in the above code:
      On select click of 1st grid view table
      Values in 2nd grid view appear from some other table.
      Now i Have to Delete values in gridview2 on click of delete button.
      But The above code shows an exception:"Valu es for one Or MOre Parameters has not Been Given"

      so Please check out

      Comment

      • talhakarkun
        New Member
        • Jun 2010
        • 12

        #4
        first u have to deleted values for that id in child tables and then after u want to delete entry from parent tables.

        if u are directly deleted parent table value then it gives exception because that value or ID uses in its child table.

        so batter way is that first u want to delete its child table entry and after deleted parent table entry

        Comment

        • jagdeep gupta
          New Member
          • Aug 2010
          • 98

          #5
          Actually the tables are arranged as:
          On The basis of first (gridview1) linked to table Year(Year, Month) when arow is selected than the values corresponding to that Month are displayed in GridView2 From Table MonthlyFee(Mont h,Class,RollNo, Year,Fee)
          Now i Have to delete values from 2nd grid view(RollNo is Unique)

          Comment

          • talhakarkun
            New Member
            • Jun 2010
            • 12

            #6
            if RollNo is the Primary key of your table then u had RollNo use in diffrent table as foreign key...
            first check it and if roll no is connected with another tables with foreign key then first delete all recored from those tables which concerned same roll no in primary table..

            like examples.
            if u want to delete roll no:2 in gridview 2.ok
            but roll no-2 is used by another table(s) as a foreign key..
            so first check it which table uses roll no-2 and delete all recored from those table(s) where roll no-2 and after delete main table recored(like u have uses in Gridview2)

            Comment

            • jagdeep gupta
              New Member
              • Aug 2010
              • 98

              #7
              No friend rollno is not acting as foreign key and
              the exception thrown is values for one or more parameter has not been given

              Comment

              • mzmishra
                Recognized Expert Contributor
                • Aug 2007
                • 390

                #8
                I think the problem is somehwere in your command queries.
                Are you able to get the values in both the grids properly.

                Comment

                • jagdeep gupta
                  New Member
                  • Aug 2010
                  • 98

                  #9
                  Yes i have got the values but the exception while deleting is "Values for one or more parameter has not been given"

                  Comment

                  Working...