Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i have the following scenario:

3 database tables: job, position and branch
position and branch are lookup tables whose primary keys are foreign keys in jobs table

In the lookup tables...when the user wants to delete a record, a field called isDeleted is set to 1 instead of deleting the record from the table

I also have a gridview whose select statement sql is the following:
SQL
SELECT Jobs.JobID, Branches.BranchName, Positions.PositionName, Jobs.YearsOfExperience, Jobs.IsArabicSpeaking, Jobs.Qualifications, Jobs.Role, Jobs.ExpireyDate, Jobs.isPublished FROM Branches INNER JOIN Jobs ON Branches.BranchID = Jobs.Branch INNER JOIN Positions ON Jobs.Position = Positions.PositionID

My Gridview :
XML
<asp:GridView ID="gvJobs" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" DataKeyNames="JobID" OnSelectedIndexChanged="gvJobs_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton Text="Edit" ID="lbEdit" OnClick="lbEdit_Click" Enabled="<%# CanBeEdited(Convert.ToInt32(gvJobs.SelectedDataKey.Value)) %>" runat="server" />
                    <span onclick="return confirm('Are you sure you want to delete?')">
                    <asp:LinkButton Text="Delete" ID="lbDelete" OnClick="lbDelete_Click" runat="server" />
                        </span>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="JobID" HeaderText="JobID" SortExpression="JobID" InsertVisible="False" ReadOnly="True" />
            <asp:BoundField DataField="BranchName" HeaderText="BranchName" SortExpression="BranchName" />
            <asp:BoundField DataField="PositionName" HeaderText="PositionName" SortExpression="PositionName" />
            <asp:BoundField DataField="YearsOfExperience" HeaderText="YearsOfExperience" SortExpression="YearsOfExperience" />
            <asp:CheckBoxField DataField="IsArabicSpeaking" HeaderText="IsArabicSpeaking" SortExpression="IsArabicSpeaking" />
            <asp:BoundField DataField="Qualifications" HeaderText="Qualifications" SortExpression="Qualifications" />
            <asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
            <asp:BoundField DataField="ExpireyDate" HeaderText="ExpireyDate" SortExpression="ExpireyDate" />
            <asp:CheckBoxField DataField="isPublished" HeaderText="isPublished" SortExpression="isPublished" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="Gray" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>

Problem:
how do i disable the edit link button in the gridview on a record that has a lookup value deleted ?

I attempted to tackle the problem by creating a method that returns a boolean and use that value to set the enabled property of the edit link button accordingly as you can see in the gridview markup above.

Method code:
C#
public bool CanBeEdited(int id)
{
   bool valid = true;
   using (var db = new safatesNewsEntities())
   {
       var tmpJob=(from j in db.Jobs
                       where j.JobID==id
                       select j).FirstOrDefault();
       int p = tmpJob.Position;
       int b = tmpJob.Branch;
       var chkBranck = (from branch in db.Branches
                        where branch.BranchID == b
                        select branch).FirstOrDefault();
       var chkPos = (from pos in db.Positions
                     where pos.PositionID == p
                     select pos).FirstOrDefault();
       if (chkBranck.isDeleted || chkPos.isDeleted)
       {
           valid = false;
       }
   }
   return valid;
}


the method takes an integer which is the job id. The method checks if it can find rows in the lookup tables that correspond to branchID and positionID foreign keys from the job record of interest that have been deleted. if the current record have any of the lookup records deleted, the method returns false.

The result of the attempt is:
Object reference not set to an instance of an object error


please let me know if you need further explanation
Many thanks :)
Posted
v2
Comments
Which line exactly throwing the exception?

i think your tmpJob is null please check the tmpJob is null or not before selecting the position and Branch
C#
var tmpJob=(from j in db.Jobs
                       where j.JobID==id
                       select j).FirstOrDefault();
if(tmpJob!=null){
       int p = tmpJob.Position;
       int b = tmpJob.Branch;
}

Hope this helps
 
Share this answer
 
Comments
Reda Makarem 29-Aug-13 1:46am    
it appears that the datakey value that is passed to the method is null
ill look further into it..any help is very appreciated :)
hello all
I have successfully solved the problem by putting some logic into the databinding method of the link button to retrieve the datakey value and then call my CanbeEdited method passing the retrieved value as a parameter.
here is the databinding method of the link button:

protected void lbEdit_DataBinding(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            GridViewRow gvr = (GridViewRow)lb.NamingContainer;
            int i = gvr.RowIndex;
            int tempID = Convert.ToInt32(gvJobs.DataKeys[i].Value);
            lb.Enabled = CanBeEdited(tempID);
        }
 
Share this answer
 
Comments
Schatak 28-Mar-14 7:17am    
It helped. Thanks

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