Ahleki's Profile
Reputation: 0
Apprentice
- Group:
- Members
- Active Posts:
- 49 (0.05 per day)
- Joined:
- 01-June 10
- Profile Views:
- 1,389
- Last Active:
Oct 17 2011 03:16 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Value of type ListItem cannot be converted to 'Valuepair'
Posted 3 Oct 2011
Frinavale, on 30 September 2011 - 04:58 PM, said:Set the Value Property of the ListItem to the ID.
Set the Text Property of the ListItem to the "Description" (Or whatever you want to display in the DropDownList).
When the SelectedIndexChanged event occurs, retrieve the SelectedValue to get the ID.
-Frinny
Ok thanks for everything I appreciate it -
In Topic: Value of type ListItem cannot be converted to 'Valuepair'
Posted 30 Sep 2011
Frinavale, on 29 September 2011 - 04:25 PM, said:Did you try the CType methods?
The thing is that you can't store the whole object in the ListItem...
You're going to have to store something that identifies the object (in the value) so that you can retrieve it again.
-Frinny
Hey thanks for everything you are a wizard. Is there a way I can hide the ID part of the listitems in the combo box. I have tried out some weird stuff but nothing seems to be working. -
In Topic: Value of type ListItem cannot be converted to 'Valuepair'
Posted 29 Sep 2011
Frinavale, on 28 September 2011 - 11:54 PM, said:Normally I don't spell out what I was hinting at but today I'm in a strange mood.
So, what I recommended before was to add code that will handle casting the ValuePair type into a ListItem type. To do this you have to implement some code for the CType operator.
This is what your code would look like for the ValuePair class:
Public Class ValuePair Public Value As Object Public Description As String Public Reference As String Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String, Optional ByVal NewRef As String = "") Value = NewValue Description = NewDescription Reference = NewRef End Sub Public Property UnderlyingValue() As String Get Return Value End Get Set(ByVal Thisvalue As String) Value = Thisvalue End Set End Property Public Overrides Function ToString() As String Return Description End Function Public Shared Narrowing Operator CType(ByVal initialData As ListItem) As ValuePair Return New ValuePair(initialData.Value, initialData.Text) End Operator Public Shared Widening Operator CType(ByVal initialData As ValuePair) As ListItem Return New ListItem(initialData.Description, initialData.Value) End Operator End Class
I modified your PopulateCombo method to test this out and it works fine:
Public Sub PopulateCombo() cboClientID.Items.Clear() 'Dim cmdDevices As SqlCommand = New SqlCommand("GetDevices", DBConnection.MyConnObj)' 'cmdDevices.CommandType = CommandType.StoredProcedure' 'With cmdDevices' ' .Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ""' ' .Parameters.Add("@ClientID", SqlDbType.VarChar).Value = "sa"' ' .Parameters.Add("@RecordDeleted", SqlDbType.Bit).Value = 0' 'End With' 'dtClients.Load(cmdDevices.ExecuteReader())' 'For Each dr As DataRow In dtClients.Rows' ' cboClientID.Items.Add(New ValuePair(dr.Item("Client_ID"), dr.Item("Client_Name")).ToString)' 'Next' For i As Integer = 1 To 10 cboClientID.Items.Add(New ValuePair(i, String.Format("{0} {1}", "Description for Item", i.ToString))) Next End Sub
There's another approach that you can take though.
You could convert your Public members into Properties to allow ASP.Net to bind the DropDownList to them. (In my opinion using using Properties is just better programming practice anyways).
Using this approach you would convert the Public members in your ValuePair class into properties like this:
Public Class ValuePair 'Public Value As Object' 'Public Description As String' 'Public Reference As String' Private _value As Object Private _description As String Private _reference As String Public Property Value() As Object Get Return _value End Get Set(ByVal value As Object) _value = value End Set End Property Public Property Description() As String Get Return _description End Get Set(ByVal value As String) _description = value End Set End Property Public Property Reference() As String Get Return _reference End Get Set(ByVal value As String) _reference = value End Set End Property Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String, Optional ByVal NewRef As String = "") Value = NewValue Description = NewDescription Reference = NewRef End Sub Public Property UnderlyingValue() As String Get Return Value End Get Set(ByVal Thisvalue As String) Value = Thisvalue End Set End Property Public Overrides Function ToString() As String Return Description End Function End Class
Then in your PopulateCombo method you would:
- Create a list of ValuePair objects based on the information retrieved from the database
- Set the comboClientID.DataSource to this list
- Set the comboClientID.DataTextField to the property that you want displayed as text (probably the "Description" property)
- Set the comboClientID.DataValueField to the property that you want to use as the value for the item (probably the reference....)
- Call the comboClientID.DataBind method to bind the data to the DropDownList
Like this:
Partial Public Class _Default Inherits System.Web.UI.Page Private _vpItems As List(Of ValuePair) Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If IsPostBack = False Then PopulateCombo() Else _vpItems = TryCast(Session("_vpItems"), List(Of ValuePair)) End If End Sub Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender cboClientID.DataSource = _vpItems cboClientID.DataTextField = "Description" cboClientID.DataValueField = "Value" cboClientID.DataBind() End Sub Public Sub PopulateCombo() cboClientID.Items.Clear() 'Dim cmdDevices As SqlCommand = New SqlCommand("GetDevices", DBConnection.MyConnObj)' 'cmdDevices.CommandType = CommandType.StoredProcedure;' 'With cmdDevices' ' .Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ""' ' .Parameters.Add("@ClientID", SqlDbType.VarChar).Value = "sa"' ' .Parameters.Add("@RecordDeleted", SqlDbType.Bit).Value = 0' 'End With' 'dtClients.Load(cmdDevices.ExecuteReader())' 'For Each dr As DataRow In dtClients.Rows' ' cboClientID.Items.Add(New ValuePair(dr.Item("Client_ID"), dr.Item("Client_Name")).ToString)' 'Next' _vpItems = New List(Of ValuePair) For i As Integer = 1 To 10 _vpItems.Add(New ValuePair(i, String.Format("{0} {1}", "Description for Item", i.ToString))) Next Session("_vpItems") = _vpItems End Sub End Class
-Frinny
Oh yeah, I wanted to tell you that you should be referring to the MSDN Library for documentation on .NET controls and other programming concepts. It has all the documentation you need to figure out how to use the ASP.NET DropDownList.
Thanks for the help but the problem still lies under the SelectedIndexChanged event of the combo box as I had stated earlier.
i tried using third party controls but I figured that as you stated ASP.NET controls do not support GDI. - Create a list of ValuePair objects based on the information retrieved from the database
-
In Topic: Value of type ListItem cannot be converted to 'Valuepair'
Posted 28 Sep 2011
BTW this code was working perfectly in VB.Net.The problem is the ASP.NET combo box does not support the ObjectCollection property whereas that of Windows Form works.
So is there any way I can achieve this.
Thanks. -
In Topic: Conversion from string "" to type 'Double' is not vali
Posted 21 Jul 2011
Am trying to fix the error by converting the value of the cell to a double
tot += CDbl(dg.Rows(i).Cells(5).Value)
And also there is no empty cell because am getting rid of the last row
This is how the values appear on the datagridview
1 EASY WEIGH KEYPAD 1 100 2 EASY WEIGH COMM PORT 1 100
Am summing the last column in this case
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
|
|


Find Topics
Find Posts
View Reputation Given

|
Comments
Ahleki has no profile comments yet. Why not say hello?