By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,961 Members | 2,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,961 IT Pros & Developers. It's quick & easy.

DataGridView System.NullReferenceException On Read Only Values

P: n/a
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 ObjectDataSource. 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.NullReferenceException 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="gvApplicantsNotMatched" runat="server"
AutoGenerateEditButton="True"
BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px"
CellPadding="3"
DataSourceID="ObjectDataSource1" AutoGenerateColumns="False"
DataKeyNames="UN,PW,DB" GridLines="Vertical">
<Columns>
<asp:BoundField DataField="ID_Num"
HeaderText="Id Number" NullDisplayText="Please Enter an ID" />
<asp:BoundField DataField="SSN"
HeaderText="SSN" ReadOnly="True" />
<asp:BoundField DataField="First_Name"
HeaderText="First Name" ReadOnly="True" />
<asp:BoundField DataField="Last_Name"
HeaderText="Last Name" ReadOnly="True" />
<asp:BoundField DataField="Birth_Date"
HeaderText="Date of Birth" ReadOnly="True" />
<asp:BoundField DataField="UN"
HeaderText="User Name" Visible="False" />
<asp:BoundField DataField="PW"
HeaderText="Password" Visible="False" />
<asp:BoundField DataField="DB"
HeaderText="Database" Visible="False" />
</Columns>
<FooterStyle BackColor="#CCCCCC"
ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black"
/>
<SelectedRowStyle BackColor="#008A8C"
Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999"
ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084"
Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1"
runat="server" SelectMethod="FindUnmachedApplicants"
TypeName="AdmissionsWebService"
UpdateMethod="UpdateApplicationInfo">

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

[WebMethod]
public DataSet FindUnmachedApplicants(UserIdentifier currentUser)
{
ConstructConnectionStringFromUserIdentifier(curren tUser);

DataSet CommonAppDataSet_Unmatched = new DataSet();
DataTable errorTable = new DataTable();

#region construct errorTable

errorTable.TableName = "Error Table";
errorTable.Columns.Add("ErrorExists",
System.Type.GetType("System.String"));
errorTable.Columns.Add("message",
System.Type.GetType("System.String"));

#endregion

string CommonAppUnmatchedDataQuery = String.Format(@"
select name_master.id_num as wrong_id,
hdx_adm_CommonApp_import_staging.Id_Num,
hdx_adm_CommonApp_import_staging.SSN,
hdx_adm_CommonApp_import_staging.First_Name,
hdx_adm_CommonApp_import_staging.Last_Name,
hdx_adm_CommonApp_import_staging.Birth_Dte as Birth_Date,
UN = '" + currentUser.UserName + @"', PW = '" +
currentUser.Password + @"',
DB = '" + currentUser.Database + @"'
from hdx_adm_CommonApp_import_staging left join name_master

on hdx_adm_CommonApp_import_staging.id_num =
name_master.id_num
where name_master.id_num is null");

try
{
Conn.Open();
SelectCmd = new SqlCommand(CommonAppUnmatchedDataQuery,
Conn);
Adapter.SelectCommand = SelectCmd;

Adapter.Fill(CommonAppDataSet_Unmatched);
Conn.Close();
}
catch (Exception ex)
{
err.Add("An error occurred in FindUnmatchedApplicants().");
err.Add(ex.ToString());
err.Add(CommonAppUnmatchedDataQuery);
DataRow errorRow = errorTable.NewRow();
errorRow["ErrorExists"] = "Error";
errorRow["message"] = err.ToString();
errorTable.Rows.Add(errorRow);
CommonAppDataSet_Unmatched.Tables.Add(errorTable);
}
return CommonAppDataSet_Unmatched;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

[WebMethod]
public void UpdateApplicationInfo(int ID_Num, string SSN, string
First_Name,
string Last_Name, string Birth_Date, string UN, string PW,
string DB)
{
bool ConnIsOpen = false;
string CommonAppUpdatedDataQuery = string.Empty;

ConnIsOpen = ConstructConnectionString(UN, PW, DB);

// try
// {
CommonAppUpdatedDataQuery = String.Format(@"
update hdx_adm_CommonApp_import_staging
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)
CommonAppUpdatedDataQuery += @" and ssn = " + SSN;
// }
// catch (Exception e2)
// {
// err.Add(@"An error occurred in UpdateApplicationInfo(int
ID_Num, string SSN, string First_Name,
// string Last_Name, string Birth_Date, string UN,
string PW, string DB).");
// err.Add(e2.ToString());
// }
try
{
Conn.Open();
UpdateCmd = new SqlCommand(CommonAppUpdatedDataQuery,
Conn);
Adapter.UpdateCommand = UpdateCmd;

Adapter.UpdateCommand.ExecuteNonQuery();
Conn.Close();
}
catch (Exception ex)
{
err.Add(@"An error occurred in UpdateApplicationInfo(int
ID_Num, string SSN, string First_Name,
string Last_Name, string Birth_Date, string UN, string
PW, string DB).");
err.Add(ex.ToString());
}
}

Sep 28 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.