Solved

DataGrid View-Urgent

Posted on 2008-10-03
25
355 Views
Last Modified: 2013-12-18
Hi
I have windows where i enter data in one windows form, based on these, i need to display the retreived data from the oracle database in a data grid view of another windows form. how do i display it
0
Comment
Question by:SGUDAPAT
  • 12
  • 12
25 Comments
 
LVL 18

Expert Comment

by:Priest04
ID: 22638453
I dont understand the quesion. You want to display data from oracle database on datagridview? Pull the data into datatable and bind datagridview to datatable.

Goran
0
 
LVL 15

Expert Comment

by:Shaju Kumbalath
ID: 22639816
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22645075
Goran: I want to display the data based the selection which I entered in the first on the second windows.
Like I enter ID From:1000
                  ID To     :2000
In first windows form, i want to display the data related to 1000 to 2000 ID in the data grid view of the second windows form.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22645128
SGUDAPAT, what exactly do you want help with?

1) Do you know how to create OracleConenction?
2) How does the SELECT query looks like?
3) Shold data be read only?
    3a) if yes, do you know how to create OracleCommand and OracleDataReader objects?
    3b) if no, do you know how to create OracleOleDbDataAdapter?
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22645135
sorry, it should have been
3b) if no, do you know how to create OracleDataAdapter?
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22645167
yes i know know to create the Oracle Data Adapter and I can also get into the data table by fill it with data set, but i am sure how i can get the data selection from one form and display it in another form(data grid)
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22645239
ok so post the code here where you create conenction and data adapter, and also tell me what field is used to filter data?

example

SELECT * FROM TableName WHERE field_name BETWEEN 1000 AND 2000
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22646398
i have the code like this
Here is where I have my connections

--------------------------

Public Function GetDataSet(ByRef ds As DataSet, _

                               ByVal sSQL As String, _

                               Optional ByVal tableName As String = "Table") As Boolean

        Try

            DA.SelectCommand = New OleDbCommand(sSQL)

            DA.SelectCommand.Connection = dbConn

            DA.Fill(ds, tableName)

            GetDataSet = True

        Catch ex As Exception

            writeErrorLog(ex)

            GetDataSet = False

        Finally

            DA.SelectCommand.Dispose()

        End Try

    End Function

---------------------------------------------------------

Here is the select query

---------------------------------------------------------

Private Sub GetSQL()

        Dim DS As New DataSet

        sAREA = SelectAreaCBx.SelectedItem.ToString.Trim

 sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _

 "storage_area, level1, level2, level3, level4, plant " & _

 " from thi_inv_tags_temp where area = '" & sAREA & "'" & _

 "and tagnum between "&STARTTxt.Text&" and "&EndTxt.Text & " order by tagnum "

        If GetDataSet(DS, sql) Then

            With DS.Tables("Table")
 

                If .Rows.Count > 0 Then

                    For Each row As DataRow In .Rows

                        With row

                        dgvCount.DataSource = DS.Tables("Table")              

                        End With

                    Next

                End If

            End With

        End If

    End Sub

---------------------------------

Now i do I get data displayed on the second form

I enter data of ranges start tag and end tag in the first form, I select area also in the first form, but I want the data from table to be displayed in the second form.

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22646592
if this data should be displayed on the 2nd form (form2), then you should place this two functions for getting dataset onto from2.


' form2 contructor

Public Sub New(startTag As Integer, endTag As Integer)

    GetSQL(startTag, endTag)

End Sub
 

Private Sub GetSQL(startTag As Integer, endTag As Integer)

        Dim DS As New DataSet

        sAREA = SelectAreaCBx.SelectedItem.ToString.Trim

 sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _

 "storage_area, level1, level2, level3, level4, plant " & _

 " from thi_inv_tags_temp where area = '" & sAREA & "'" & _

 "and tagnum between "& startTag & " and " & endTag & " order by tagnum "

        If GetDataSet(DS, sql) Then

            With DS.Tables("Table")

 

                If .Rows.Count > 0 Then

                    For Each row As DataRow In .Rows

                        With row

                        dgvCount.DataSource = DS.Tables("Table")              

                        End With

                    Next

                End If

            End With

        End If

    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22646594
of course, GetDataSet, should also be placed on Form2
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22646607
will startTag and endTag values be imported directly from form1, since they are entered in form1, or should I make them public in form1
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22646696
you will pass them to form 2 over its constructor. ex

dim Form2 as New Form2(Convert.ToInt32(STARTTxt.Text), Convert.ToInt32(EndTxt.Text))
Form2.Show() ' or ShowDialog, which ever you prefer.

You could have also created SQLString in form1, and pass query to form2 (contructor would  then receive only one parameter - SQLString as String), makes no difference.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22646854
u mean to say that if I want the form2 to open when i click on Submit button in form1, then i should write in the button click:
dim Form2 as New Form2(Convert.ToInt32(STARTTxt.Text), Convert.ToInt32(EndTxt.Text))
Form2.Show()
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22647281
yes
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22649728
I really for your appreciate for your patience to explain me the possible solution.

This whole application contains of 8 windows forms.
I have Main where I hae have toolStrip on which I have lables,
The user can select the label he wants and that form opens.
When I am adding the above code in the form1 and form2
I am getting an unhandled exception like

"InhandledOperationEception was unhandled

An error occured creation the form.See Exception.InnerException for the details.the error is:Oject reference not set to an instance of an object."

I am gtting this exception on beow line in the toolStripMenuItemClick event.
FrmCnt .ShowDialog()
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22649874
what is the rest of the code for this event?
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22649901
Here is the code
  Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click

        If isAdmin = 1 Then

            With FrmCnt

                .ShowDialog()

            End With

        ElseIf isAdmin = 2 Then

            With FrmCnt

                .ShowDialog()

            End With

        Else
 

            With FrmLogInScreen

                .ShowDialog()

            End With

        End If

    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22649985
I worked before added the code I am not sure what went wrong, here is the code once with i added.

And also near the constructore new keyword should call initialize component method.
In the button click of form1

-------------------------------- 

Dim FrmEnterCountData As New FrmEnterCountData(Stag, ETag, sAREA)

 FrmEnterCountData.ShowDialog()

-------------------------

form2

-------------------------

 Public Sub New(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)

        GetSQL(startTag, endTag, sAREA)

    End Sub
 

    Private Sub GetSQL(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)

        Dim DS As New DataSet

        sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _

        "storage_area, level1, level2, level3, level4, plant " & _

        " from tags_temp where area = '" & sAREA & "'" & _

        "and tagnum between " & startTag & " and " & endTag & " order by tagnum "

-------------

        If GetDataSet(DS, sql) Then

            With DS.Tables("Table")
 

                If .Rows.Count > 0 Then

                    For Each row As DataRow In .Rows

                        With row

                            dgvCount.DataSource = DS.Tables("Table")

                        End With

                    Next

                End If

            End With

        End If

    End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
Priest04 earned 500 total points
ID: 22650268
SGUDAPAT, you didnt understand what we are doing here. So I will try to explain again.

>>And also near the constructore new keyword should call initialize component method.

You are correct about this, add this line to constructor. Now to the explanation

Requirement was to pass information from one form to another (ex from Form1 to Form2). One of the ways to pass information from one form to another is to pass information to Form2 constructor.

When Form is created, a contructor is being called, and as you have pointed above, in constructor InitializeComponent() procedure is being called (this proeedure is located in designer code), which leads to all controls that are placed on the form (like buttons, textboxes, etc) being initialized. Default constructor does not accept any parameters (note: in VB, default constructor is not shown in code, but when code is compiled, it is automatically added, because it must exist)

Public Sub New()
    InitializeComponent()
End Sub

we need to change this contructor to access parameters in order to pass information. Since we need to pass 2 paramteters, we will add two parameters


Public Sub New(startTag As Integer, endTag As Integer)
    InitializeComponent()
End Sub

and this will allow us to pass information. How we do it. To create an instance of the form with DEFAULT contructor, we use this code

Dim frm As New Form1() ' note that between parentheses there is no parameter, since we call the defailt construcot.

At this point, a form and all its controls are being instantiated, but still not visible. To make the form visible, we call either Show or ShowDialog methods.

Now, lets see how we should instantiate form with contructor that accepts parameters, like in above example

Public Sub New(startTag As Integer, endTag As Integer)
    InitializeComponent()
End Sub

As you can see contructor takes 2 paramteres of type integer. So, when creating form object, we need to pass 2 parameters

Dim frm As New Form1(5, 10) ' we pass two parameters with values of 5 and 10)
frm.ShowDialog()

so, in your case, it should be

If isAdmin = 1 Then
    Dim frm As New FrmCnt(Convert.ToInt32(STARTtxt,Text), Convert.ToInt32(EndTxt.Text))
            With FrmCnt
                .ShowDialog()
            End With
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22650694
Thanks a bunch, i got it working.
I am have final question for you, i think its a simple one, but i don't know how to do it.
i have the windows form, on which i display my data grid view, i set that windowSatae property to Maximized, so that it open like a maximized window that window open, but my data grid view is not getting maximized with the window, how do I autosize the data grid view.
0
 
LVL 1

Author Closing Comment

by:SGUDAPAT
ID: 31502933
got it working, thanks once again
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22651104
Set Anchor property to Top, Left, Right and Bottom.
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22651254
If my data grid grid view returns zero rows i want it prompt me that i need to enter range of tags that exist.
I tried it using this code

It is giving me a OjectDisposedException wasn unhandled on the
FrmEnter.showDialog
in the form1
What should I do?
form2---------------------

----------------

  Private Sub GetSQL(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)

        Dim DS As New DataSet

        sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _

        "storage_area, level1, level2, level3, level4, plant " & _

        " from tags_temp where area = '" & sAREA & "'" & _

        "and tagnum between " & startTag & " and " & endTag & " order by tagnum "

        If GetDataSet(DS, sql) Then

            With DS.Tables("Table")
 

                If .Rows.Count > 0 Then

                    dgvCount.DataSource = DS.Tables("Table")

                    rowLbl.Text = "Rows : " & .Rows.Count

                Else

                     Me.Close()

                    Me.Dispose()

                End If

            End With

        End If

    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22651517
SGUDAPAT, we could go on like this forever. You should create new threads for new questions.
0
 
LVL 1

Author Comment

by:SGUDAPAT
ID: 22651558
:) seems like.
thanks Goran,figured it out, don't need a new thread right now :))
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now