DataViewHelp

A

Adam

Hi
I have problem with:
I would like that DataView read from DataBaza "lastname"
But I don't now how do it
My code is

Collection CreateDataSource()
{
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);
String strCmd = "SELECT * FROM Employees ";
SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
dr1.Read();
//MoreInfo.Text = BuildMoreInfoText(dr);



/////////////////////////////////////////////
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;


dr1.Close();
conn.Close();

Please, help me
 
C

Chris, Master of All Things Insignificant

I don't understand your question. Can you explain what you need?

Chris
 
A

Adam Macko

Ok
Sorry
This code read dr[0] and dr[1] from
DataTable dt = new DataTable();

DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < SqlDataReader dr1.Read(); i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

but I would like that this code read from table "lastname" from database

String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);
String strCmd = "SELECT * FROM Employees ";
SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
dr1.Read();
//MoreInfo.Text = BuildMoreInfoText(dr);
What I have to do it
Please, help me
 
A

Adam Macko

Ok
Sorry
This code read dr[0] and dr[1] from
DataTable dt = new DataTable();

DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < SqlDataReader dr1.Read(); i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

but I would like that this code read from table "lastname" from database

String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);
String strCmd = "SELECT * FROM Employees ";
SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
dr1.Read();
//MoreInfo.Text = BuildMoreInfoText(dr);
What I have to do it
Please, help me
 
C

Chris, Master of All Things Insignificant

Ok, I don't do C# much so I might make a syntax error here or there and it
won't be the right case-sensitivity... I think this is what you want to do.

String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);
String strCmd = "SELECT lastname FROM Employees "; <-Changed
SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
//dr1.Read(); <-Changed


DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

// for (int i = 0; i < SqlDataReader dr1.Read(); i++) <--Changed
do While dr1.Read() <- this may be a VB thing, just loop
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = dr1.GetString(0) <-Changed
//or this would work
dr[1] = dr1.GetString("lastname")
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

Hope this helps
Chris
 
C

Cor Ligthert

Adam,
Collection CreateDataSource()
{
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);
String strCmd = "SELECT LastName FROM Employees ";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(strCmd, strConn);
da.Fill(dt);
DataView dv = new DataView(dt);
return dv;
}

This looks more simple to me.

In the dataview is now a column that is
dv[x]["LastName"] or dv[x][0]

I hope this helps?

Cor
 

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

Similar Threads


Top