Hello Everyone,
I have been trying to figure this one out for two days now. I have
created a DataGridView which is populated by an ObjectDataSourc e. My
problem occurs when I attempt to use the Edit feature of the
DataGridView. I have set several of the BoundField elements of the
DataGridView to ReadOnly="True" . When I attempt to use the Edit
feature to UPDATE the rows in the DataGridView I get a
System.NullRefe renceException for each of the ReadOnly BoundField
elements. When the BoundField is set to ReadOnly="False " the error
does not occur. I am including the code I use to populate the
DataGridView as well as the UPDATE code and the DataGridView ASP.NET
code as well. Thank you for your help in advance.
<asp:GridView ID="gvApplicant sNotMatched" runat="server"
AutoGenerateEdi tButton="True"
BackColor="Whit e" BorderColor="#9 99999"
BorderStyle="No ne" BorderWidth="1p x"
CellPadding="3"
DataSourceID="O bjectDataSource 1" AutoGenerateCol umns="False"
DataKeyNames="U N,PW,DB" GridLines="Vert ical">
<Columns>
<asp:BoundFie ld DataField="ID_N um"
HeaderText="Id Number" NullDisplayText ="Please Enter an ID" />
<asp:BoundFie ld DataField="SSN"
HeaderText="SSN " ReadOnly="True" />
<asp:BoundFie ld DataField="Firs t_Name"
HeaderText="Fir st Name" ReadOnly="True" />
<asp:BoundFie ld DataField="Last _Name"
HeaderText="Las t Name" ReadOnly="True" />
<asp:BoundFie ld DataField="Birt h_Date"
HeaderText="Dat e of Birth" ReadOnly="True" />
<asp:BoundFie ld DataField="UN"
HeaderText="Use r Name" Visible="False" />
<asp:BoundFie ld DataField="PW"
HeaderText="Pas sword" Visible="False" />
<asp:BoundFie ld DataField="DB"
HeaderText="Dat abase" Visible="False" />
</Columns>
<FooterStyle BackColor="#CCC CCC"
ForeColor="Blac k" />
<RowStyle BackColor="#EEE EEE" ForeColor="Blac k"
/>
<SelectedRowSty le BackColor="#008 A8C"
Font-Bold="True" ForeColor="Whit e" />
<PagerStyle BackColor="#999 999"
ForeColor="Blac k" HorizontalAlign ="Center" />
<HeaderStyle BackColor="#000 084"
Font-Bold="True" ForeColor="Whit e" />
<AlternatingRow Style BackColor="Gain sboro" />
</asp:GridView>
<asp:ObjectData Source ID="ObjectDataS ource1"
runat="server" SelectMethod="F indUnmachedAppl icants"
TypeName="Admis sionsWebService "
UpdateMethod="U pdateApplicatio nInfo">
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[WebMethod]
public DataSet FindUnmachedApp licants(UserIde ntifier currentUser)
{
ConstructConnec tionStringFromU serIdentifier(c urrentUser);
DataSet CommonAppDataSe t_Unmatched = new DataSet();
DataTable errorTable = new DataTable();
#region construct errorTable
errorTable.Tabl eName = "Error Table";
errorTable.Colu mns.Add("ErrorE xists",
System.Type.Get Type("System.St ring"));
errorTable.Colu mns.Add("messag e",
System.Type.Get Type("System.St ring"));
#endregion
string CommonAppUnmatc hedDataQuery = String.Format(@ "
select name_master.id_ num as wrong_id,
hdx_adm_CommonA pp_import_stagi ng.Id_Num,
hdx_adm_CommonA pp_import_stagi ng.SSN,
hdx_adm_CommonA pp_import_stagi ng.First_Name,
hdx_adm_CommonA pp_import_stagi ng.Last_Name,
hdx_adm_CommonA pp_import_stagi ng.Birth_Dte as Birth_Date,
UN = '" + currentUser.Use rName + @"', PW = '" +
currentUser.Pas sword + @"',
DB = '" + currentUser.Dat abase + @"'
from hdx_adm_CommonA pp_import_stagi ng left join name_master
on hdx_adm_CommonA pp_import_stagi ng.id_num =
name_master.id_ num
where name_master.id_ num is null");
try
{
Conn.Open();
SelectCmd = new SqlCommand(Comm onAppUnmatchedD ataQuery,
Conn);
Adapter.SelectC ommand = SelectCmd;
Adapter.Fill(Co mmonAppDataSet_ Unmatched);
Conn.Close();
}
catch (Exception ex)
{
err.Add("An error occurred in FindUnmatchedAp plicants().");
err.Add(ex.ToSt ring());
err.Add(CommonA ppUnmatchedData Query);
DataRow errorRow = errorTable.NewR ow();
errorRow["ErrorExist s"] = "Error";
errorRow["message"] = err.ToString();
errorTable.Rows .Add(errorRow);
CommonAppDataSe t_Unmatched.Tab les.Add(errorTa ble);
}
return CommonAppDataSe t_Unmatched;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[WebMethod]
public void UpdateApplicati onInfo(int ID_Num, string SSN, string
First_Name,
string Last_Name, string Birth_Date, string UN, string PW,
string DB)
{
bool ConnIsOpen = false;
string CommonAppUpdate dDataQuery = string.Empty;
ConnIsOpen = ConstructConnec tionString(UN, PW, DB);
// try
// {
CommonAppUpdate dDataQuery = String.Format(@ "
update hdx_adm_CommonA pp_import_stagi ng
set id_num = " + ID_Num + @"
where first_name = '" + First_Name.Trim () + @"'
and last_name = '" + Last_Name.Trim( ) + @"' and birth_dte =
'" + Birth_Date.Trim () + @"'");
if (SSN.Length == 9)
CommonAppUpdate dDataQuery += @" and ssn = " + SSN;
// }
// catch (Exception e2)
// {
// err.Add(@"An error occurred in UpdateApplicati onInfo(int
ID_Num, string SSN, string First_Name,
// string Last_Name, string Birth_Date, string UN,
string PW, string DB).");
// err.Add(e2.ToSt ring());
// }
try
{
Conn.Open();
UpdateCmd = new SqlCommand(Comm onAppUpdatedDat aQuery,
Conn);
Adapter.UpdateC ommand = UpdateCmd;
Adapter.UpdateC ommand.ExecuteN onQuery();
Conn.Close();
}
catch (Exception ex)
{
err.Add(@"An error occurred in UpdateApplicati onInfo(int
ID_Num, string SSN, string First_Name,
string Last_Name, string Birth_Date, string UN, string
PW, string DB).");
err.Add(ex.ToSt ring());
}
}