Please help with some code.

A

Adrian

There is an error in the line marked XXX
Could someone put it right for me?
Many thanks,
Adrian.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{

#region Windows Form Designer generated code
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(784, 238);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(816, 266);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
using (SqlConnection my_connection = new SqlConnection("server =
fred\\sqlexpress; database = TrialDB; Integrated Security = TRUE;"))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1 "+
"SELECT Column_one, Column_two, Column_three FROM dbo.Table_2";

using(SqlDataAdapter my_adapter = new
SqlDataAdapter(query,my_connection))
{

using(DataSet data_set = new DataSet())
{
my_adapter.TableMappings.Add("Tab", "bdo.Table_1");
my_adapter.TableMappings.Add("Tab1", "bdo.Table_2");
my_adapter.Fill(data_set,"Tab");

DataTable my_table = data_set.Tables["Tab"];

string display_string = string.Empty;

foreach(DataRow row in
my_table.Rows)XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}
}
 
A

Adrian

And the error is?
That I should have looped through the tables
as I have subsequently discovered.
Adrian.

Cor Ligthert said:
And the error is?

:)

Cor

Adrian said:
There is an error in the line marked XXX
Could someone put it right for me?
Many thanks,
Adrian.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{

#region Windows Form Designer generated code
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(784, 238);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(816, 266);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
using (SqlConnection my_connection = new SqlConnection("server =
fred\\sqlexpress; database = TrialDB; Integrated Security = TRUE;"))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1 "+
"SELECT Column_one, Column_two, Column_three FROM dbo.Table_2";

using(SqlDataAdapter my_adapter = new
SqlDataAdapter(query,my_connection))
{

using(DataSet data_set = new DataSet())
{
my_adapter.TableMappings.Add("Tab", "bdo.Table_1");
my_adapter.TableMappings.Add("Tab1", "bdo.Table_2");
my_adapter.Fill(data_set,"Tab");

DataTable my_table = data_set.Tables["Tab"];

string display_string = string.Empty;

foreach(DataRow row in
my_table.Rows)XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}
}
 
C

Cor Ligthert [MVP]

Adrian,

I did mean what was the error that was given.
There could be more reasons for that (empty table etc)

Cor

Adrian said:
And the error is?
That I should have looped through the tables
as I have subsequently discovered.
Adrian.

Cor Ligthert said:
And the error is?

:)

Cor

Adrian said:
There is an error in the line marked XXX
Could someone put it right for me?
Many thanks,
Adrian.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{

#region Windows Form Designer generated code
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(784, 238);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(816, 266);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
using (SqlConnection my_connection = new SqlConnection("server =
fred\\sqlexpress; database = TrialDB; Integrated Security = TRUE;"))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1 "+
"SELECT Column_one, Column_two, Column_three FROM dbo.Table_2";

using(SqlDataAdapter my_adapter = new
SqlDataAdapter(query,my_connection))
{

using(DataSet data_set = new DataSet())
{
my_adapter.TableMappings.Add("Tab", "bdo.Table_1");
my_adapter.TableMappings.Add("Tab1", "bdo.Table_2");
my_adapter.Fill(data_set,"Tab");

DataTable my_table = data_set.Tables["Tab"];

string display_string = string.Empty;

foreach(DataRow row in
my_table.Rows)XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}
}
 
A

Adrian

I did mean what was the error that was given.
There could be more reasons for that (empty table etc)

Cor

This seems to work ok.
No missing table.
IMO the loop was missing.

Adrian

private void Form1_Load(object sender, System.EventArgs e)
{
using (SqlConnection my_connection = new SqlConnection("server =
fred\\sqlexpress; database = TrialDB; Integrated Security = TRUE;"))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1 "+
"SELECT Column_one, Column_two, Column_three FROM dbo.Table_2" ;

using(SqlDataAdapter my_adapter = new SqlDataAdapter(query,my_connection))
{

using(DataSet data_set = new DataSet())
{
my_adapter.TableMappings.Add("Tab", "bdo.Table_1");
my_adapter.TableMappings.Add("Tab1", "bdo.Table_2");
my_adapter.Fill(data_set,"Tab");
for(int t = 0; t<2; t++)
{
DataTable my_table = data_set.Tables[t];
string display_string = string.Empty;
foreach(DataRow row in my_table.Rows)
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top