Update dataset without using datagridview

FreeriderUK

Well-known member
Joined
Jun 27, 2006
Messages
100
Location
London
Programming Experience
10+
I want to update a field in SQL database to hold a NULL value.

I can do this using a DataGridView control, with the code:
VB.NET:
Expand Collapse Copy
OrdersDataGridView.CurrentRow.Cells(0).Value = DBNull.Value
Which works fine, but I don't need the DataGridView on the form.
I could hide it behind another control, but seems a bit messy.

How can I achieve the same thing but without having to use a DataGridView?

The field in question holds a Date value displayed in a TextBox - if that makes any difference.

Probably posted in wrong section...
 
So, you have a textbox bound to the MyColumn column of a bindingsource BS bound to a MyDataTable DT. You filled the datatable with at least one row and now you see the entry in the box. You want to set it null.



DirectCast(DirectCast(BS.Current, DataRowView).Row, MyDataTableRow).SetMyColumnNull()

On forms where a bindingsource is present, I usually make a property:

VB.NET:
Expand Collapse Copy
Private ReadOnly Property CurrentRow()
  Get
    Return DirectCast(DirectCast(BS.Current, DataRowView).Row, MyDataTableRow)
  End Get
End Property

Makes the code nicer (VB.NET is UGLY with casting.. actually its ugly to a C programmer period .. ;) )like:

CurrentROw.SetMyPropertyNull()

if currentRow.IsMyOtherColumnNull() Then
currentRow.MyOtherCOlumn = "A value!"


etc
 
Thanks cjard - this seems to be working.

Although it complains when I try to close the form:
"Object reference not set to an instance of an object."
 
Putting
VB.NET:
Expand Collapse Copy
if currentRow.IsMyOtherColumnNull() Then
currentRow.MyOtherCOlumn = "A value!"
in a Try...Catch block seems to have fixed it.
 
For brevity, I neglected to mention that my properties for current row usually look like:

Get
If bs.CurrentRow Is Nothing THen return Nothing
Else (return the double casted thing)


handy tip, in using the debugger. CLick debug menu, click expcetions, tick next to CLR exceptions in the THROWN column. Now you will be notified as soon as a exception is thrown, whether or not it is handled, and before it has travelelld. this can help you find the exact point an error originates
 
Back
Top