Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table
tblClass - ClassId,Class
tblSection- SectionId,Section

cmbClass will display the class saved in tblClass (value member-classId, display member-Class)
when i will click on save button it will saved the value member cmbClass and a txtbox value to tblSection. And a datagridview dgvSection will show the SectionId,Class,Section . How to display the class in dgvSection? give me a easy way to do this. thnk u.
Here is my code -

VB
' LOAD COMBOBOX ITEM FROM TBLCLASS
Public Sub loadclass()
        con.Open()
        LoadClassCmd = New SqlCommand("SELECT * FROM tblClass ", Con)
        Dim adpt As New SqlDataAdapter(LoadClassCmd)
        Dim dt As New DataTable
        adpt.Fill(dt)
        cmbClass.DataSource = dt
        cmbClass.DisplayMember = "Class"
        cmbClass.ValueMember = "ClassId"
        con.Close()
    End Sub

'LOAD DGV FROM TBLSECTION
Public Sub loaddgvSec()
        con.Open()
        Dim adpt As New SqlDataAdapter("SELECT * FROM tblSection", Con)
        Dim ds As New DataSet()
        adpt.Fill(ds)
        dgvSection.DataSource = ds.Tables(0)
        con.Close()
    End Sub

Private Sub frmClassMstr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        
        loaddgvSec()
        loadclass()
    End Sub

Private Sub btnSecAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSecAdd.Click
'SAVE BUTTON
InsertSql = "INSERT INTO tblSection values('" & cmbClass.SelectedValue & "','"& txtSection.Text & "')"
        insertcmd = New SqlCommand(insertsql, con)  
            Try
                con.Open()

                insertcmd.ExecuteNonQuery()
                MsgBox("Section '" & txtSection.Text & "' in Class '" & cmbClass.Text & "' Inserted Successfully", MsgBoxStyle.Information)
                txtSection.Clear()
                cmbClass.Focus()

            Catch ex As Exception
                MsgBox(ex.Message())
            Finally
                con.Close()
        End Try
loaddgvSec()


End Sub
Posted
Updated 24-Dec-13 1:32am
v2
Comments
Peter Leow 24-Dec-13 7:47am    
What problems did you encounter? Your code looks fine except this:
MsgBox("Section '" & txtSection.Text & "' in Class '" & cmbClass.SelectedText & "' Inserted Successfully", MsgBoxStyle.Information)
Member 10457175 24-Dec-13 9:11am    
my datagridview class column shows the value of classid saved in tblsection. but i want to show the class not classId.
Peter Leow 24-Dec-13 9:21am    
That is because you did not ask for it in your sql query, see my solution.

Is This Correct way?


VB
Public Sub loaddgvSec()
        con.Open()
        Dim adpt As New SqlDataAdapter("SELECT SectionId,(Select Class From tblClass WHERE tblSection.ClassId=tblClass.ClassId)AS Class,Section FROM tblSection", Con)
        Dim ds As New DataSet()
        adpt.Fill(ds)
        dgvSection.DataSource = ds.Tables(0)
        
        con.Close()
    End Sub
 
Share this answer
 
I see, now it is clearer, change the following sql query in your code
Dim adpt As New SqlDataAdapter("SELECT * FROM tblSection", Con)

to
Dim adpt As New SqlDataAdapter("SELECT SectionId, Class, Section FROM tblSection inner join tblClass on ClassId = SectionId", Con)


then you should be to get a datagridview with Class and Section columns.
 
Share this answer
 
v2
Comments
Member 10457175 24-Dec-13 14:01pm    
Is this code ok?


Select s.SectionId,c.Class,s.Section from tblClass c,tblSection s where c.ClassId = s.ClassId


is there any other code?
how to do this code with module?
Peter Leow 24-Dec-13 21:24pm    
Yes, it will work as it is the same as inner join.
If I have already provided solution for this question, please accept it. If you have another question, please post it as a new question.
Member 10457175 24-Dec-13 14:04pm    
if user delete any calss row from tblClass then datagridview will show nothing. is there any solution for this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900