D
dtblankenship
Hello everyone,
I know this question has been asked many times in the forums, and after
spending a few days reading, I am still confused as to the answer.
I have a ListBox (lstBox), SqlConnection (sqlConnection),
SqlDataAdapter (daLookupData), SqlDataAdapter (daData), DataSet
(dsLookupData), and DataSet (dsData), all created via the IDE during
design-time. Here is the design of my simple tables: (An example
since I can't post my real tables, etc.)
LookupDataTable
----------------
LookupDataID - int (identity)
LookupDataText - nvarchar(25)
Data
-----
LookupDataID - int
(Other nvarchar textbox nullable fields)
Each DataAdapter has a command that simply selects * from each table,
respectively.
Here are the properties I've edited for lstBox:
DataMember: LookupDataTable (An existing table)
DataSource: dsLookupData
DataTextField: LookupDataText (nvarchar(25))
DataValueField: LookupDataID (int)
I then went to DataBindings and changed the SelectedValue to be:
DataBinder.Eval(dsData, "Tables[Data].DefaultView.[0].LookupDataID",
"{0:N}")
Here is my Page_Load:
//----------------------------------------------
sqlConnection.Open();
if (!Page.IsPostBack)
{
daLookupData.Fill(dsLookupData);
daData.Fill(dsData);
Page.DataBind();
}
sqlConnection.Close();
//----------------------------------------------
Since there is no data in the Data table, a null value is returned to
the lstBox's SelectedValue. Since that value doesn't exist, the web
application bombs stating:
"Specified argument was out of the range of valid values. Parameter
name: value "
and it's pointing to the Page.DataBind() as the error line;
I know it is the SelectedValue because when I unbind that property, it
all works as intended. I have also attempted changing to the ISNULL
T-SQL recommended on some posts, but there is no -1 or 0 value, so it
still bombs. I am unable to have an extra entry in the listbox--only
those values currently in the LookupDataTable can be shown in the list.
I know there exists a problem where null values are causing web
applications to crash like this, and I have read many ways to fix it,
but none seem to work. I still would like to use databinding to
accomplish the loading of the data into these controls and the setting
of values for each record, but would not mind overloading a function or
creating an event handler for this, but do not know what ones would be
needed. I've also seen code where you check for null to set it to
DBNull.value or an empty string, but where exactly does that code go in
this situation? I have seend DataBinding classes that do not exist in
the web forms, only in Windows forms, so that is out of the question
(unless I'm mistaken).
Basically, if a null value exists, I need nothing to be selected, and
if one does, then I need that one to be selected.
I since added a button to check the current value of this box unbound,
and it seems to be an empty string (''), however, when I try to
ISNULL(...) to a nvarchar empty string, it does not work (yes, I did
change the type in the xsd file).
Has anyone found a solution for this problem while still using
databinding for the datasource AND selectedvalue, and if
events/overloaded functions were used to accomplish this, what ones
should I research? My thought was if I could overload the function
that sets the value at the time of databinding, so I could trap the
error, I could just not set the selected value and continue on,
allowing the next databind to get the value if the next one existed.
Thank you all in advance.
David
I know this question has been asked many times in the forums, and after
spending a few days reading, I am still confused as to the answer.
I have a ListBox (lstBox), SqlConnection (sqlConnection),
SqlDataAdapter (daLookupData), SqlDataAdapter (daData), DataSet
(dsLookupData), and DataSet (dsData), all created via the IDE during
design-time. Here is the design of my simple tables: (An example
since I can't post my real tables, etc.)
LookupDataTable
----------------
LookupDataID - int (identity)
LookupDataText - nvarchar(25)
Data
-----
LookupDataID - int
(Other nvarchar textbox nullable fields)
Each DataAdapter has a command that simply selects * from each table,
respectively.
Here are the properties I've edited for lstBox:
DataMember: LookupDataTable (An existing table)
DataSource: dsLookupData
DataTextField: LookupDataText (nvarchar(25))
DataValueField: LookupDataID (int)
I then went to DataBindings and changed the SelectedValue to be:
DataBinder.Eval(dsData, "Tables[Data].DefaultView.[0].LookupDataID",
"{0:N}")
Here is my Page_Load:
//----------------------------------------------
sqlConnection.Open();
if (!Page.IsPostBack)
{
daLookupData.Fill(dsLookupData);
daData.Fill(dsData);
Page.DataBind();
}
sqlConnection.Close();
//----------------------------------------------
Since there is no data in the Data table, a null value is returned to
the lstBox's SelectedValue. Since that value doesn't exist, the web
application bombs stating:
"Specified argument was out of the range of valid values. Parameter
name: value "
and it's pointing to the Page.DataBind() as the error line;
I know it is the SelectedValue because when I unbind that property, it
all works as intended. I have also attempted changing to the ISNULL
T-SQL recommended on some posts, but there is no -1 or 0 value, so it
still bombs. I am unable to have an extra entry in the listbox--only
those values currently in the LookupDataTable can be shown in the list.
I know there exists a problem where null values are causing web
applications to crash like this, and I have read many ways to fix it,
but none seem to work. I still would like to use databinding to
accomplish the loading of the data into these controls and the setting
of values for each record, but would not mind overloading a function or
creating an event handler for this, but do not know what ones would be
needed. I've also seen code where you check for null to set it to
DBNull.value or an empty string, but where exactly does that code go in
this situation? I have seend DataBinding classes that do not exist in
the web forms, only in Windows forms, so that is out of the question
(unless I'm mistaken).
Basically, if a null value exists, I need nothing to be selected, and
if one does, then I need that one to be selected.
I since added a button to check the current value of this box unbound,
and it seems to be an empty string (''), however, when I try to
ISNULL(...) to a nvarchar empty string, it does not work (yes, I did
change the type in the xsd file).
Has anyone found a solution for this problem while still using
databinding for the datasource AND selectedvalue, and if
events/overloaded functions were used to accomplish this, what ones
should I research? My thought was if I could overload the function
that sets the value at the time of databinding, so I could trap the
error, I could just not set the selected value and continue on,
allowing the next databind to get the value if the next one existed.
Thank you all in advance.
David