I can't think of a way to get rows out of a table.

It was easy doing it with VB.Net and I'm just finding vc++ really difficult.

This is what I have so far.
I've connected to the database file and I am able to get the data shown onto the datagrid.

SQLiteConnection^ ObjConnection = gcnew SQLiteConnection("Data Source=SwiftService.db3;");
SQLiteCommand^ ObjCommand = gcnew SQLiteCommand("SELECT * FROM Contact", ObjConnection);
ObjCommand->CommandType = CommandType::Text;
SQLiteDataAdapter^ ObjDataAdapter = gcnew SQLiteDataAdapter(ObjCommand);
DataSet^ dataSet = gcnew DataSet();
ObjDataAdapter->Fill(dataSet, "Contact");
DataTable table = gcnew DataTable("Contact");
dataGridView1->DataSource = dataSet->Tables["Contact"];

Instead of showing the data in the datagrid, I want to do a for each statement, which creates a panel with some labels inside it for each row in the table.

I've done something like this in VB.Net, which is shown below.

Private Sub CheckStockLevel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = GetConnect()
        conn.Open()
        Try

            'Select statement
            Dim comm As New SqlCommand("SELECT * FROM Part", conn)
            Dim myReader As SqlDataReader = comm.ExecuteReader  'create datareader and execute sql statement
            Dim dt As New DataTable()   'create a Data Table
            dt.Load(myReader)   'Load the data into the data table

            Dim lblArray As Label() = New Label(10) {}   'create an array of labels
            Dim nRow As DataRow
            Dim y As Integer = 0  'value of the label y-position
            Dim no As Integer = 1   'value of the "number" label
            Dim total As Integer = 0
            Dim nfont As New Font("Microsoft Sans Serif", 12, FontStyle.Regular)    'label font

            'Loop through each row in the datatable
            For Each nRow In dt.Rows
                Dim i As Integer = 0
                'Loop through each array in the label array and set the properties
                For i = 0 To UBound(lblArray)
                    lblArray(i) = New Label
                    lblArray(i).AutoSize = True
                    lblArray(i).BackColor = System.Drawing.Color.Transparent
                    lblArray(i).Font = nfont
                    Panel1.AutoScroll = True
                    'Me.Controls.Add(lblArray(i))
                    Me.Panel1.Controls.Add(lblArray(i))
                Next
                'set the label properties
                lblArray(0).Text = no
                lblArray(1).Text = nRow.Item(0).ToString()
                lblArray(2).Text = nRow.Item(1).ToString()
                lblArray(3).Text = nRow.Item(2).ToString()
                lblArray(4).Text = nRow.Item(3).ToString()
                lblArray(5).Text = nRow.Item(4).ToString()
                lblArray(6).Text = nRow.Item(5).ToString()
                lblArray(7).Text = nRow.Item(7).ToString()
                lblArray(8).Text = nRow.Item(6).ToString()
                lblArray(9).Text = nRow.Item(8).ToString()

                lblArray(0).Location = New System.Drawing.Point(5, y)
                lblArray(1).Location = New System.Drawing.Point(33, y)
                lblArray(2).Location = New System.Drawing.Point(126, y)
                lblArray(3).Location = New System.Drawing.Point(242, y)
                lblArray(4).Location = New System.Drawing.Point(323, y)
                lblArray(5).Location = New System.Drawing.Point(457, y)
                lblArray(6).Location = New System.Drawing.Point(531, y)
                lblArray(7).Location = New System.Drawing.Point(670, y)
                lblArray(8).Location = New System.Drawing.Point(750, y)
                lblArray(9).Location = New System.Drawing.Point(855, y)

                total += nRow.Item(7)
                no += 1
                y = y + 30
            Next
            lblTotal.Text = total  'total stock label
            lblParts.Text = dt.Rows.Count  'total parts label
        Catch ex As Exception
            MessageBox.Show("No records found", "Search")
            MessageBox.Show(ex.ToString)

        End Try
        conn.Close()
    End Sub

Thanks

Here is an example program

Thanks for the link. I've had a look through it, but I'm doing my project on Windows Forms application, so I'm not too sure on how I can incorporate the code example you linked to.

I've already got the connection set up, it's just a matter of knowing how to use that connection to manipulate the database.

How did you compile sqlite.c with windows Forms project? VC++ 2010 Express gives me errors that it can not be compiled with CLI/C++.

I'm using VC++ 2008. The steps I followed were:

http://sqlite.phxsoftware.com/

Installing the file from there (currently SQLite-1.0.66.0-setup.exe) you can then do the following.

In a new windows Forms project in Visual Studio, go to Project, Properties, click the Add New Reference button, Browse and find the file System.Data.SQLite.dll. It should be at

C:\Program Files (x86)\SQLite.NET\bin

Make sure you also click the "COPY LOCAL" to true, so that the DLL gets put in the exe folder when it compiles. Now that that is added to the project, go to the Design view of your form, go into toolbox and drag in a DataGridView under Data. Note that this is just for an example - you should come up with your own way to visualise your database (e.g. form fields!).

View code on Form1.h and

at the top, enter in the using namespace block


using namespace System::Data::SQLite;

Then you have the connection part which I posted above.

Ok, I've figured some stuff out.

for each (DataRow ^row in dataSet->Tables["Contact"]->Rows)
{
	int posX;
	posX = 200;
	int posY;
	posY = 200;

	TextBox ^txtBox = gcnew TextBox();
	txtBox->Location = Point(posX, posY);

	posY += 100;
	this->Controls->Add(txtBox);
				
	MessageBox::Show(row->default[0]->ToString());
}

What happens here is that when I move onto the form that contains this code, it shows some messageboxes for the first column of each row. So if i have 3 rows in my database, it will show 3 messageboxes. This is just for testing purposes.

What I'm trying to do, is to dynamically create some controls for each row in the database. So I'm initially trying this with just one textbox. My problem is that only one textbox is created on the form. I'm not sure why 3 textboxes aren't being created (because there are 3 rows in the table "Contact").

Any ideas? Thanks.

Instead of dynamically creating text boxes try using a grid control Here's a free one for C# that you might be able to use in CLI/CPP, I don't know because I have never tried it.

>> My problem is that only one textbox is created on the form. I'm not sure why 3 textboxes aren't being created (because there are 3 rows in the table "Contact").

It looks like the boxes are created on top of each other, i.e. effectively the location of each box is Point(200, 200) .

Check wxSQLite3

Not really sure why you posted that?

Instead of dynamically creating text boxes try using a grid control Here's a free one for C# that you might be able to use in CLI/CPP, I don't know because I have never tried it.

I'm not too sure if this would be good with what I'm trying to do.

I think I need to clarify on what exactly I'm trying to do. Basically, in my database I will have a list of contacts; name, address, telephone, Customer/Supplier.

What I want to do is list all those contacts with their details on the Form. Ideally by creating a panel with labels for each contact. So it will look something like the contacts list on phones.

It looks like the boxes are created on top of each other, i.e. effectively the location of each box is Point(200, 200) .

I thought that too, but I don't think that's the problem because the box is located at posX and posY. After each for each loop, posY should be incremented by 100. So all the boxes should be at the same x position but be 100 pixels(?) apart in the y position.

Thanks for the help guys, appreciate it.

>> After each for each loop, posY should be incremented by 100. So all the boxes should be at the same x position but be 100 pixels(?) apart in the y position.

You missed the point .. incrementing posY has no practical effect in your code w.r.t. positioning the boxes. Try changing to following ..

// Initial values - outside the loop
int posX = 200;
int posY = 200;

for each (DataRow ^row in dataSet->Tables["Contact"]->Rows)
{
	TextBox ^txtBox = gcnew TextBox();
	txtBox->Location = Point(posX, posY);

	posY += 100;
	this->Controls->Add(txtBox);
				
	MessageBox::Show(row->default[0]->ToString());
}

:), you did it. Thanks alot!
Thanks everyone, appreciate it once again.

Not really sure why you posted that?

Sorry I misunderstood your post :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.