Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,064 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




Set variable with Primary key from colum database

 
Reply to this topicStart new topic

Set variable with Primary key from colum database

Hanzie
29 Feb, 2008 - 11:14 AM
Post #1

D.I.C Head
**

Joined: 19 Aug, 2007
Posts: 92


My Contributions
Hello,

I have a database-table with a colum wich contains ID-numbers as integers. These ID-numbers in this column is set as primary keys.

Also I have a listbox which contains names from another column in the same database-table.

I now want to set a variable (as integer) which contains this primary key.
So if you select the name in the listbox the variable gets the primary key number from the name-item.

Something like this:

CODE

Private Sub MyListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ID as integer
        ID = MyListBox.SelectedValue

    End Sub


I have set in the propertybox from the listbox the selectedvalue as my primary-key column from the database. This must be correct!!

When the integer-variable ID is set i want to use this number (integer) for my selectquery:

CODE

Dim strsql As String = "select Name where MyID=ID"


But when I try this I get the folowing error-code:

Column 'ID' is constrained to be unique. Value '9' is already present.


What to do about this?? I'm in the dark here!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 11:24 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well first your SQL statement is incorrect, it should be


vb

Dim strsql As String = "SELECT Name FROM YourTable WHERE MyID=" & ID


To ensure the SelectedValue holds the correct value can you show the code you're using to populate the DropDownList, and make sure to put it inside code tags like so code.gif
User is offlineProfile CardPM
+Quote Post

Hanzie
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 11:59 AM
Post #3

D.I.C Head
**

Joined: 19 Aug, 2007
Posts: 92


My Contributions
The select-query was an example. It works when i try it a different way.

I don't have an exact code for the listbox.
In Visual Studio 2005 from the menu "datasources" I have taken the table i needed as a listbox and imported it to my form.

When i start the program the listbox is filled with the data i needed so that should be correct?


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 12:03 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well I found one error and that was your SQL statement, and I showed an example of how it should look. The only thig I havent seen is how you're executing the statement, so if you want to make sure it's correct you can either just test it with the new SQL statement (replace "YourTable" wiht the name of your table), or you can post the code you're using to execute that statement smile.gif
User is offlineProfile CardPM
+Quote Post

Hanzie
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 12:20 PM
Post #5

D.I.C Head
**

Joined: 19 Aug, 2007
Posts: 92


My Contributions
Ok, here is the complete code:

CODE

Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports System.Windows.Forms



Public Class FrmTekeningen

    Dim strGeselecteerdeBedrijven As String = ""
    'Create a variable to hold your parameter value  
    Dim BedrID As Integer

    Private Sub TekeningenBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.TekeningenBindingSource.EndEdit()
        Me.TekeningenTableAdapter.Update(Me.DocRegDataDataSet.Tekeningen)

    End Sub

    Private Sub BedrijvenIDListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BedrijvenIDListBox.SelectedIndexChanged
        BedrID = BedrijvenIDListBox.SelectedValue

    End Sub


    Private Sub FrmTekeningen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DocRegDataDataSet.Bedrijven' table. You can move, or remove it, as needed.
        Me.BedrijvenTableAdapter.Fill(Me.DocRegDataDataSet.Bedrijven)
        'TODO: This line of code loads data into the 'DocRegDataDataSet.Tekeningen' table. You can move, or remove it, as needed.
        Me.TekeningenTableAdapter.Alle_Tekeningen(Me.DocRegDataDataSet.Tekeningen)

    End Sub

    Private Sub BtnBedrijven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBedrijven.Click
        Me.Visible = False
        My.Forms.Hoofdmenu.Show()

    End Sub

  

    Private Sub BtnTekeningenBedrijf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTekeningenBedrijf.Click
        'Create your query as you already have done  
        Dim strsql As String = "select bladnummer, naam, datum from tekeningen where BedrijvenID=@BedrijfID"
        'Get your connection string (You've done this right)  
        Dim strconnectionstring As String = My.Settings.DocRegDataConnectionString
        'Create your SqlConnection (Done)  
        Dim objconnection As New SqlConnection(strconnectionstring)
        'Create your SqlCommand (done)  
        Dim objcommand As New SqlCommand()

        '**Set your command properties**  
        With objcommand
            .CommandText = strsql
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@BedrijfID", BedrID)
            .Connection = objconnection
        End With
        'Create a new SqlDataAdapter  
        Dim objdataAdapter As New SqlDataAdapter()
        'Set the SelectCommand property of our adapter  
        objdataAdapter.SelectCommand = objcommand
        'Create a new DataSet  
        Dim objdataset As New DataSet
        'Fill the DataSet using the SqlDataAdapter  
        objdataAdapter.Fill(objdataset, "Tekeningenbedrijven")

        Dim objbindingsource As New BindingSource
        objbindingsource.DataSource = objdataset.Tables("Tekeningenbedrijven")

        TekeningenDataGridView.DataSource = objbindingsource
        objdataset.Clear()

    End Sub

End Class



I get the folowing error:

Column 'BedrijvenID' is constrained to be unique. Value '2' is already present.


Please help!
User is offlineProfile CardPM
+Quote Post

Hanzie
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 12:31 PM
Post #6

D.I.C Head
**

Joined: 19 Aug, 2007
Posts: 92


My Contributions
It looks like the program is trying to set a value to my database-column "BedrijvenID".
Because this databasecolumn is set as a unique primary key, it can not by assigned a value again.

But i'm not trying to give it a value, I wanna READ the value and get it in my select-query.

Look at attachment, is print screen of my listbox-settings.

When I change the listbox-select codeline in:

CODE

BedrID = CType(BedrijvenIDListBox.SelectedItem(), Integer)


I get the folowing error:

Conversion from type 'DataRowView' to type 'Integer' is not valid.

Maybe i'm a step further, but how to solve this? Is Ctype not the correct code?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Set Variable With Primary Key From Colum Database
29 Feb, 2008 - 02:20 PM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You cannot convert the actual DataRowView to an integer, but you can convert it's value to an Integer as the SelectedItem() value will always be DataRowView, which cannot be converted. Use the SelectedValue.ToString(), like so


vb

BedrID = CType(BedrijvenIDListBox.SelectedValue.ToString(), Integer)

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month