Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > vb ado 2 question on binding bit
 

vb ado 2 question on binding bit

Hi,

i am new to ado 2.0 in VB.

The database table has a bit column and not null

how do i bind a radio /check box button to assign true when NOT clicked (or clicked)?
another word, the radio button will have a default value of false. is it possible? Thanks.


CREATE TABLE [dbo].[testBit](
[active] [bit] NOT NULL
) ON [PRIMARY]


System.Data.NoNullAllowedException was unhandled
Message="Column 'active' does not allow nulls."
Source="System.Data"
StackTrace:
at System.Data.DataColumn.CheckNullable(DataRow row)
at System.Data.DataColumn.CheckColumnConstraint(DataRow row, DataRowAction action)
at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent)
at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Int32 position, Boolean fireEvent, Exception& deferredException)
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos, Boolean fireEvent)
at System.Data.DataView.FinishAddNew(Boolean success)
at System.Data.DataRowView.EndEdit()
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.BindingSource.EndEdit()
at testBit.Form1.TestBitBindingNavigatorSaveItem_Click(Object sender, EventArgs e) in c:\Projects\myServer\testBit\testBit\Form1.vb:line 5
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at testBit.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
  • Moved byVMazurMVPTuesday, July 28, 2009 10:39 AM (From:ADO.NET Data Providers)
  •  
light_wt  Tuesday, July 28, 2009 7:31 AM

Hi light_wt,

Yes, it is possible to bind a RadioButton/CheckBox to a bool value. We can create a BindingSource to bind a RadioButton/CheckBox to the Boolean column. This is a code snippet shows how to bind two radio buttons:

Private _bindSrc As BindingSource = Nothing
    Private Sub RadioButtonBind_Load(ByVal sender As Object, ByVal e As EventArgs)
        'Retrieve the data. 
        Dim table As DataTable = Me.GetDataTable()
        table.Rows.Add(table.NewRow())
        'Initial the binding source. 
        _bindSrc = New BindingSource()
        _bindSrc.DataSource = table
        'Bind the first radio button. "Checked" is the property, "active" is the column name of type binary. 
        Me.radioButton1.DataBindings.Add("Checked", _bindSrc, "active")
        'Add this handler to synchronize the state of the second radio button. 
        AddHandler Me.radioButton1.CheckedChanged, AddressOf radioButton1_CheckedChanged
    End Sub

    Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.radioButton2.Checked = Not Me.radioButton1.Checked
    End Sub

We can bind a check box in a similar way:

'Move to the next row. 
_bindSrc.MoveNext() 

We can move to another recore through BindingSource. For example:

'Move to the next row.
_bindSrc.MoveNext() 


You can get more information about BindingSource from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx.

From my experience, the error you met is caused by adding a new row in which the value of the boolean column is not set. For example:

'This would cause the error you met 
table.Rows.Add(table.NewRow()) 


We need to set the value of the boolean column as follow:

Dim table As DataTable
Dim row As DataRow = table.NewRow()
row("activate") = False
table.Rows.Add(row)



Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 29, 2009 8:08 AM
Hi,
Try this
 if (database.bitvalue == "1")
            {
                radioButton1.Checked = true;
            }
            else
            {
                radioButton1.Checked = false;
            }
for setting default value for radio button.in form loading event or design time set the property checked false or true
Cheers, C.Gnanadurai
  • Proposed As Answer byGnanadurai Tuesday, July 28, 2009 2:56 PM
  •  
Gnanadurai  Tuesday, July 28, 2009 2:15 PM

Hi light_wt,

Yes, it is possible to bind a RadioButton/CheckBox to a bool value. We can create a BindingSource to bind a RadioButton/CheckBox to the Boolean column. This is a code snippet shows how to bind two radio buttons:

Private _bindSrc As BindingSource = Nothing
    Private Sub RadioButtonBind_Load(ByVal sender As Object, ByVal e As EventArgs)
        'Retrieve the data. 
        Dim table As DataTable = Me.GetDataTable()
        table.Rows.Add(table.NewRow())
        'Initial the binding source. 
        _bindSrc = New BindingSource()
        _bindSrc.DataSource = table
        'Bind the first radio button. "Checked" is the property, "active" is the column name of type binary. 
        Me.radioButton1.DataBindings.Add("Checked", _bindSrc, "active")
        'Add this handler to synchronize the state of the second radio button. 
        AddHandler Me.radioButton1.CheckedChanged, AddressOf radioButton1_CheckedChanged
    End Sub

    Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.radioButton2.Checked = Not Me.radioButton1.Checked
    End Sub

We can bind a check box in a similar way:

'Move to the next row. 
_bindSrc.MoveNext() 

We can move to another recore through BindingSource. For example:

'Move to the next row.
_bindSrc.MoveNext() 


You can get more information about BindingSource from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx.

From my experience, the error you met is caused by adding a new row in which the value of the boolean column is not set. For example:

'This would cause the error you met 
table.Rows.Add(table.NewRow()) 


We need to set the value of the boolean column as follow:

Dim table As DataTable
Dim row As DataRow = table.NewRow()
row("activate") = False
table.Rows.Add(row)



Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 29, 2009 8:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Combo box data bind
• format a column into a datagrid using a typed collection
• How can I list all IList members of an object progammatically
• Formatting a date
• select row from gridview
• identifying all child object's state?
• Underlying deletion not updating the display of the selected row in the DataGridView
• Invisible Column
• DataGridView Nested Subgrid
• How to insert data in datagrid into empty sql table