0

This is my select SQL syntax to display the category choices, but what I need to do is display the corresponding category chosen based on equipment_id

I got 2 tables, tblOfficeEquipmentCategory and tblOfficeEquipmentProfile

I need to inner join tblOfficeEquipmentProfile so I can add the WHERE equipment_id = '"txtid.text"'

What would be the corresponding SQL syntax

Public Sub DisplayCategory()
    'based on oe_id
    Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim sqlcommand As SqlCommand

    sqlconn.Open()
    Dim da As New SqlDataAdapter("select * from tblOfficeEquipmentCategory", sqlconn)
    Dim dt As New DataTable
    da.Fill(dt)
    cmbCategory.DataSource = dt
    cmbCategory.ValueMember = "CAT_Name"
    cmbCategory.DisplayMember = "CAT_ID"
    sqlconn.Close()
End Sub
0

4 Answers 4

1

Assuming equipment_id is located on table tblOfficeEquipmentProfile and a column CAT_ID which links it to table tblOfficeEquipmentCategory.

SELECT  a.CAT_Name
FROM    tblOfficeEquipmentCategory a
        INNER JOIN tblOfficeEquipmentProfile b
            ON a.CAT_ID = b.CAT_ID
WHERE   b.equipment_id = @ID

To further gain more knowledge about joins, kindly visit the link below:

Sign up to request clarification or add additional context in comments.

3 Comments

i will try sir, im really not used in to using parameters, but thanks for the info
i have seen in your previous question that you ask about parameters :D please do use it.
still configuring out on how to make it work sir im still debugging the code and parameters
0

Best to start thinking about how to separate your data access from your presentation code. You need something like this:

Public Function DisplayCategory() 
    cmbCategory.ValueMember = "CAT_Name"
    cmbCategory.DisplayMember = "CAT_ID"
    cmbCategory.DataSource = GetEquipmentCategories(Convert.ToInt32(txtid.text))
End Sub

Public Function GetEquipmentCategories(ByVal EquipmentID As Integer) As DataTable
    'based on oe_id

    Dim sql As String = "SELECT a.CAT_ID, a.CAT_Name" & _ 
                       " FROM tblOfficeEquipmentCategory a" & _
                       " INNER JOIN tblOfficeEquipmentProfile b ON a.CAT_ID = b.CAT_ID" & _
                       " WHERE b.equipment_id = @ID"

    Dim result As New DataTable
    Using cn New SqlConnection("server=SKPI-APPS1;Database=EOEMS;integrated security=true"), _
          cmd As new SqlCommand(sql, cn)

        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = EquipmentID

        cn.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader
            result.Load(rdr)
        End Using
    End Using
    Return result
End Function

2 Comments

sir ID contains char and integer ex. oeq-su-001 what would be the corresponding sqldbtype
Match the sqldbtype to the column definition in sql server. Look here for the list of possibilities: msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx
0
        Dim sql = "Select  Authors.Au_ID, Authors.Author, [Title Author].ISBN FROM(Authors INNER JOIN [Title Author] ON Authors.Au_ID = [Title Author].Au_ID)"

Comments

0
   Try
        con.Open()
        Dim cmd As New OleDb.OleDbCommand
        Dim dt As New DataTable
        Dim da As New OleDb.OleDbDataAdapter
        Dim sql = "Select  Authors.Au_ID, Authors.Author, [Title Author].ISBN FROM(Authors INNER JOIN [Title Author] ON Authors.Au_ID = [Title Author].Au_ID)"
        cmd.Connection = con
        cmd.CommandText = sql
        da.SelectCommand = cmd
        da.Fill(dt)
        DGV2.DataSource = dt
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.