Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Mon, 11 Aug 2008 13:31:54 +0800,    group: microsoft.public.dotnet.framework.windowsforms.databinding        back       


Radio Button Programmatic Binding   
Dear Sir/Madam,

How do I bind a radio button to a Dataset? Please help me out.

This Code generates an error Message.

IsActive is a boolean Field on an SQL Table

Me.rbActive.DataBindings.Add("checked", tblCompanyDataTable, "IsActive")

Thanks in advance for your reply..

Regards,

Cypher Drive
date: Mon, 11 Aug 2008 13:31:54 +0800   author:   CypherDrive am

RE: Radio Button Programmatic Binding   
Dear Cypher,

As I understand, there's a boolean field in your table, when you bind this 
field to the Checked property of the RadioButton control, an error occurs.

The code you wrote seems not problem, to help you resolve this problem, I 
would like to know the following information:

1. What error message did you get?
2. What kind of database did you use? As far as I know there' no 
bool/boolean type in the SQL Server 2000 or 2005.

I'm looking forward to hearing from you.

Have a nice day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues 
where an initial response from the community or a Microsoft Support 
Engineer within 1 business day is acceptable. Please note that each follow 
up response may take approximately 2 business days as the support 
professional working with you may need further investigation to reach the 
most efficient resolution. The offering is not appropriate for situations 
that require urgent, real-time or phone-based interactions or complex 
project analysis and dump analysis issues. Issues of this nature are best 
handled working with a dedicated Microsoft Support Engineer by contacting 
Microsoft Customer Support Services (CSS) at 
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
date: Mon, 11 Aug 2008 11:41:57 GMT   author:   (Zhi-Xin Ye [MSFT])

Re: Radio Button Programmatic Binding   
Zhi,

1. I'm using MS SQL Express Edition 2005
2. I'm using the Data Type Bit.
3. The Error Message is ...

    Invalid Cast Exception was unhandled
    Object cannot be cast from DBNull to other types.

Will wait for your response.

Thanks,

Cypher Drive.





"Zhi-Xin Ye [MSFT]"  wrote in message 
news:MZpNJd6%23IHA.1696@TK2MSFTNGHUB02.phx.gbl...
> Dear Cypher,
>
> As I understand, there's a boolean field in your table, when you bind this
> field to the Checked property of the RadioButton control, an error occurs.
>
> The code you wrote seems not problem, to help you resolve this problem, I
> would like to know the following information:
>
> 1. What error message did you get?
> 2. What kind of database did you use? As far as I know there' no
> bool/boolean type in the SQL Server 2000 or 2005.
>
> I'm looking forward to hearing from you.
>
> Have a nice day!
>
> Sincerely,
> Zhi-Xin Ye
> Microsoft Managed Newsgroup Support Team
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no 
> rights.
>
date: Wed, 13 Aug 2008 08:54:26 +0800   author:   CypherDrive am

Re: Radio Button Programmatic Binding   
Dear Cypher,

Thanks for the information.

The Checked property of the RadioButton control is defined as bool, so only 
boolean values can be assigned to this property. When there are null values 
in the IsActive field, they're treated as DbNull objects when performing 
the data binding. The cast from DbNull type to bool type is invalid, 
resulting in this error message.

To avoid this error message, you can try two approach:

1. Handle the Format event of the Binding object to assign a boolen value 
for the Checked property when the ConvertEventArgs.Value is DbNull.Value.

        private void Form1_Load(object sender, EventArgs e)
        {
            string connstr = 
Properties.Settings.Default.Database1ConnectionString;
            
            SqlConnection conn = new SqlConnection(connstr);
            string sql = "SELECT * From Table1";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "test");

            Binding binding = new Binding("checked", ds.Tables[0], 
"IsActive");
            binding.Format += new ConvertEventHandler(binding_Format);
            this.radioButton1.DataBindings.Add(binding);
        }

        void binding_Format(object sender, ConvertEventArgs e)
        {
            if (e.Value == DBNull.Value)
            {
                e.Value = false;//or e.Value = true; 
            }
        }

2. Change the table definition to not allow null values on the IsActive 
field.

Please try my suggestions and let me know the result. If anything is 
unclear, don't hesitate to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Wed, 13 Aug 2008 03:23:52 GMT   author:   (Zhi-Xin Ye [MSFT])

Re: Radio Button Programmatic Binding   
Xin,

I'm sorry but do you have a vb.net 2005 code. I'm not that familiar with C#.

I dont understand this part.

Binding binding = new Binding("checked", ds.Tables[0],
> "IsActive");
>            binding.Format += new ConvertEventHandler(binding_Format);
>            this.radioButton1.DataBindings.Add(binding);
>        }
>
>        void binding_Format(object sender, ConvertEventArgs e)
>        {
>            if (e.Value == DBNull.Value)
>            {
>                e.Value = false;//or e.Value = true;
>            }
>        }

Thanks,

Cypher Drive



"Zhi-Xin Ye [MSFT]"  wrote in message 
news:hXBMGQP$IHA.1688@TK2MSFTNGHUB02.phx.gbl...
> Dear Cypher,
>
> Thanks for the information.
>
> The Checked property of the RadioButton control is defined as bool, so 
> only
> boolean values can be assigned to this property. When there are null 
> values
> in the IsActive field, they're treated as DbNull objects when performing
> the data binding. The cast from DbNull type to bool type is invalid,
> resulting in this error message.
>
> To avoid this error message, you can try two approach:
>
> 1. Handle the Format event of the Binding object to assign a boolen value
> for the Checked property when the ConvertEventArgs.Value is DbNull.Value.
>
>        private void Form1_Load(object sender, EventArgs e)
>        {
>            string connstr =
> Properties.Settings.Default.Database1ConnectionString;
>
>            SqlConnection conn = new SqlConnection(connstr);
>            string sql = "SELECT * From Table1";
>            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
>            DataSet ds = new DataSet();
>            da.Fill(ds, "test");
>
>            Binding binding = new Binding("checked", ds.Tables[0],
> "IsActive");
>            binding.Format += new ConvertEventHandler(binding_Format);
>            this.radioButton1.DataBindings.Add(binding);
>        }
>
>        void binding_Format(object sender, ConvertEventArgs e)
>        {
>            if (e.Value == DBNull.Value)
>            {
>                e.Value = false;//or e.Value = true;
>            }
>        }
>
> 2. Change the table definition to not allow null values on the IsActive
> field.
>
> Please try my suggestions and let me know the result. If anything is
> unclear, don't hesitate to let me know.
>
> Sincerely,
> Zhi-Xin Ye
> Microsoft Managed Newsgroup Support Team
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> This posting is provided "AS IS" with no warranties, and confers no 
> rights.
>
date: Wed, 13 Aug 2008 13:15:40 +0800   author:   CypherDrive am

Re: Radio Button Programmatic Binding   
Dear Cypher,

I translate the code into VB.NET for your information:

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) 
Handles MyBase.Load
        Dim connstr As String = 
My.MySettings.Default.Database1ConnectionString2
        Dim conn As SqlConnection = New SqlConnection(connstr)
        Dim sql As String = "SELECT * From Table1"
        Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
        Dim ds As DataSet = New DataSet
        da.Fill(ds, "test")
        Dim binding As Binding = New Binding("checked", ds.Tables(0), 
"IsActive")
        AddHandler binding.Format, AddressOf Me.binding_Format
        Me.RadioButton1.DataBindings.Add(binding)
    End Sub

    Private Sub binding_Format(ByVal sender As Object, ByVal e As 
ConvertEventArgs)
        If (e.Value Is DBNull.Value) Then
            e.Value = False
            'or e.Value = true; 
        End If
    End Sub

If need further help on this, please let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Wed, 13 Aug 2008 10:16:57 GMT   author:   (Zhi-Xin Ye [MSFT])

Re: Radio Button Programmatic Binding   
Dear Ye,

Thanks so much.

Regards,
CYp Drv.


"Zhi-Xin Ye [MSFT]"  wrote in message 
news:l9vw72S$IHA.3988@TK2MSFTNGHUB02.phx.gbl...
> Dear Cypher,
>
> I translate the code into VB.NET for your information:
>
> Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs)
> Handles MyBase.Load
>        Dim connstr As String =
> My.MySettings.Default.Database1ConnectionString2
>        Dim conn As SqlConnection = New SqlConnection(connstr)
>        Dim sql As String = "SELECT * From Table1"
>        Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
>        Dim ds As DataSet = New DataSet
>        da.Fill(ds, "test")
>        Dim binding As Binding = New Binding("checked", ds.Tables(0),
> "IsActive")
>        AddHandler binding.Format, AddressOf Me.binding_Format
>        Me.RadioButton1.DataBindings.Add(binding)
>    End Sub
>
>    Private Sub binding_Format(ByVal sender As Object, ByVal e As
> ConvertEventArgs)
>        If (e.Value Is DBNull.Value) Then
>            e.Value = False
>            'or e.Value = true;
>        End If
>    End Sub
>
> If need further help on this, please let me know.
>
> Sincerely,
> Zhi-Xin Ye
> Microsoft Managed Newsgroup Support Team
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> This posting is provided "AS IS" with no warranties, and confers no 
> rights.
>
>
date: Fri, 29 Aug 2008 16:39:16 +0800   author:   Cypher Drive

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us