By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

DataViewHelp

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Adam,

If you mean you want to have one field in DataView to
store data 'lastname' from Employees table, you can do it
as follows:

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();

DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof
(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof
(string)));
dt.Columns.Add(new DataColumn("LastName", typeof(string)));
int i = 0;
while(dr1.Read())
{
dr = dt.NewRow();
dr["IntegerValue"] = i;
dr["StringValue"] = "Item " + i;
dr["LastName"] = dr1["lastname"];
dt.Rows.Add(dr);
i++;
}
dr1.Close();
conn.Close();

return dt.DefaultView;

Hope it helps,

Elton Wang
el********@hotmail.com

-----Original Message-----
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
.

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.