Adding all entries to combobox in linq to sql - sql

All i want to do is add each entry of my specific database table to a combo box on form load. This is what i have so far.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dc = New DataworldDataContext.DataworldDataContext
Dim countStates = (From z In dc.tblstates Select z).Count
Dim listStates = (From z In dc.tblstates Select z).ToList
For i = 0 To countStates - 1
cmbDealerState.Items.Add(listStates)
i = i + 1
Next
End Sub
Im getting the error "Items collection cannot be modified when the DataSource property is set." How can i fix this

For your 2nd problem, the combo box will have 2 properties called DisplayMember and ValueMember. The display one is the text you wish to display and the value the (usually numeric) value you return when an item is selected. Simply set them:
cmbDealerState.DisplayMember = "myDisplay"
cmbDealerState.ValueMember = "myValue"
I am presuming you are using WinForms here. If its WPF then its a bit more tricky.
Edit
dc = New DataworldDataContext.DataworldDataContext
Dim countStates = (From z In dc.tblstates Select z.StateCode).Count
Dim listStates = (From z In dc.tblstates Select z.StateCode).ToList
cmbDealerState.DisplayMember = "StateCode"
cmbDealerState.ValueMember = "StateCode"
This still gives me nothing
The problem you have is that you are trying to set DisplayMember and ValueMember to be a ToString representation of your list. In your database you will have columns that identify the data name, for example
StateID StateName
1 New York
2 Washington
The values you need to set therefore would be:
cmbDealerState.DisplayMember = "StateName"
cmbDealerState.ValueMember = "StateID"
Obviously these names are made up, but you get the idea.
When I tried this (in C#) I needed to access cmbDealerState.SelectedItem to get the values that had been selected.
Still Nothing

dc = New DataworldDataContext.DataworldDataContext
Dim listStates = (From z In dc.tblstates Select z.StateCode).ToList()
cmbDealerState.DataSource = listStates
This worked

Related

VB.NET ComboBox, setting the right "value"

I'm working on a hobby project where I will store Sail Boats, Skipper, Clubs, Races and Results. A Skipper is a person that is a member in a club.
So I have table with persons (skippers) and a table with clubs.
I have created an DataGridView where I have the person and the id to the club. I have also added a ComboBox with clubs. When I click on a row I want to load the person into several TextBoxes and also set the right club in the ComboBox, but I can't find how.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myTable As DataTable = New DataTable
Dim MyRow As DataRow
myTable = FillCluTable() <== Get clubs into a DataTable
MyRow = myTable.NewRow
MyRow(0) = 0
MyRow(1) = "Sel"
MyRow(2) = "Select Club"
myTable.Rows.InsertAt(MyRow, 0)
cbxClubs.DataSource = myTable
cbxClubs.DisplayMember = myTable.Columns("ClubName").ToString
cbxClubs.ValueMember = myTable.Columns("Id").ToString
cbxClubs.SelectedIndex = 0
End Sub
Try with:
cbxClubs.DisplayMember = "ClubName"
cbxClubs.ValueMember = "Id"
Delete the part with MyRow and build the Table header from the designer page

how to add multiple selected items from CheckedListBox to SQL table in Visual Basic (VS2019)

I currently have the following that allows me to select whats in my CheckedListBox and it displays in a message box popup
public Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
If CheckedListBox1.Items.Count > 0 Then
Dim Items As New List(Of String)
For Index As Int32 = 0 To (CheckedListBox1.Items.Count - 1)
If CheckedListBox1.GetItemChecked(Index) Then
Items.Add(CStr(CheckedListBox1.Items.Item(Index)))
End If
Next
Dim Result = String.Join(",", Items.ToArray)
'MessageBox.Show(Result)
Else
' Nothing checked
End If
Now i need that result set to be loaded to the parameter defined from my stored procedure :
' Add Multiple selections to Brand column in SQL.
sqlCommand.Parameters.Add(New SqlParameter("#SKU", SqlDbType.VarChar, 100))
sqlCommand.Parameters("#SKU").Value = CheckedListBox1.Text
Works fine with one selected value only.
but I am needing it to load the Dim Result into the #SKU parameter.
assuming example: sqlCommand.Parameters("#SKU").Value = (Result)
Regards
I figured it out. I allowed the Result set to load into a Label1:
Dim Result = String.Join(",", Items.ToArray)
Label1.Text = Result
Then I used the Label1.text to send through a variable to my stored proc:
sqlCommand.Parameters.Add(New SqlParameter("#SKU", SqlDbType.VarChar,100))
sqlCommand.Parameters("#SKU").Value = Label1.Text

Values from database based on two combo boxes into text box

I am trying to put a value based on two cascading combo boxes from a database table into a text box. Filling the two cascading combo boxes works. However when I try to put the result in the text box I run into trouble. I am using the following code :
Private Sub CmbPlaasnaam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbPlaasnaam.SelectedIndexChanged
If CmbAliasnaam.SelectedIndex > -1 AndAlso CmbPlaasnaam.SelectedIndex > -1 Then
Dim aliasnaam As String =
CmbAliasnaam.Items(CmbAliasnaam.SelectedIndex).ToString
Dim plaasnaam As String =
CmbPlaasnaam.Items(CmbPlaasnaam.SelectedIndex).ToString
Dim qry = From zc As SkeduleringsDatabasis6DataSet.OesskattingsRow In SkeduleringsDatabasis6DataSet.Oesskattings
Where zc.Aliasnaam = aliasnaam AndAlso
zc.Plaasnaam = plaasnaam
Select zc.Plaasnommer
txtPlaasnommer.Text = zc.plaasnommer
End If
End Sub
Use this line :
txtPlaasnommer.Text = qry.FirstOrDefault.ToString()
zc on your last line does not exists
you should use qry variable
like:
if qry.Count>0 Then
txtPlaasnommer.Text = qry.first
End If
zc is the inner linq variable and qry is the result that you should use

Access a panel from another form with increment variable name

Public Sub StatusLoad(ByVal id As String)
''Dim F As New Apps
''Dim W As Panel = DirectCast(F.Controls(id), Panel)
Dim count As Long = 0
Dim strQueryStat As String = "SELECT * FROM sample_db.ronda WHERE id = '" + id + "'"
Dim SqlCmdStat As New MySqlCommand(strQueryStat, dbCon)
Reader = SqlCmdStat.ExecuteReader
While Reader.Read
count = count + 1
MsgBox("id" & count)
Dim status_db = Reader.GetString("status")
If status_db = 0 Then
Apps.id.BackColor = Color.Green '<------
MsgBox("Green")
Else
Apps.BackColor = Color.Red
MsgBox("Red")
End If
End While
End Sub
I want to use 'id' variable that get from other form to find the panel name from the other form. the above code is in a class. the panel that i want to access is at another form.
at the other for i send "con.StatusLoad(id)". the name of panel is id1 , id2 , id3 ...... the error is at the arrow
Private Sub app_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ManageConnection(False)
For index As Integer = 1 To 2
Dim id As String = "id" & index
con.StatusLoad(id)
Next index
End Sub
Each control, including forms, has a Controls property that contains a collection of child controls. You can index that collection by ordinal or name, e.g.
For i = 1 To 5
Dim pnl = Me.Controls("Panel" & i)
'...
Next
Note that you'll be getting a Control reference back, so you can only use it to access members of the Control class. If you want to access members of a specific type of control then you must cast as that type.
Controls is a public member so it can be accessed from outside the form but probably shouldn't be. The same "rules" apply as would always apply to making changes to controls on a form, i.e. best practice dictates that it's done inside that form only.

VB.NET-Keep the dropdownlist status without puting the code in "Not IsPostBack"

I have 2 dropdownlists "countries" and "cities", with 2 datasources:
If a user chooses the first index in the ddl countries, he can see all the cities in the world in the ddl cities (uses datasource1).
If a user chooses a country, he can see the cities correspond to this country chosen (uses datasource2).
I put the code for changing datasource in the code behind using vb.net, in Page_Load, but after the user chose a city, and click the submit button, the dropdownlist cities can't keep the status, it goes to the first index of this ddl.
I tried to put this code in the If Not IdPostBack, but like this, it doesn't change datasource while it can keep status of the dropdownlist.
So does anyone have an idea about this problem?
I put the code here as reference:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ddlCities As DropDownList
Dim ddlCountries As DropDownList
ddlCities = CType(PN_Search.FindControl("DropDownList_Cities"), System.Web.UI.WebControls.DropDownList)
ddlCountries = CType(PN_Search.FindControl("DropDownList_Countries"), System.Web.UI.WebControls.DropDownList)
Dim countrySelect As String
countrySelect = ddlCountries.SelectedValue
Dim rechercheCitiesNull As String = "SELECT * FROM Cities WHERE id_city=1"
Dim rechercheCitiesNotNull As String = "SELECT * FROM [View_Country_City] Where id_country=" & countrySelect
If countrySelect = "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNull
ddlCities.EnableViewState = True
ddlCountries.EnableViewState = True
ddlCities.DataBind()
ElseIf countrySelect <> "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNotNull
ddlCities.DataBind()
End If
End Sub
Thanks in advance!
Ziliu
EnableViewState and it should fix your issue. Keep your binding withing a not page.ispostback.