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:
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 :
<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:
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 :)