|
|
|
date: Mon, 11 Aug 2008 13:31:54 +0800,
group: microsoft.public.dotnet.framework.windowsforms.databinding
back
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
|
|