newbieal
asked on
fill DataView in code (vs2003, c#)
How do I fill a DataView with data in code?
ASKER
DataView does not have DataSource as an option.
Here's my code as a ref.:
Here's my code as a ref.:
private void DataBind()
{
DataView dv = new DataView();
ddl1.Items.Clear();
string listtxt = string.Empty;
for(int i=0; i<dv.Table.Rows.Count; i++)
{
listtxt = dv[i]["A"].ToString();
ListItem item = new ListItem(listtxt, dv[i]["A"].ToString());
ddl1.Items.Add(item);
}
ddl1.DataBind();
ddl1.SelectedValue = Request.QueryString["id"];
}
ASKER
This is what I have in terms of trying to populate the DataView, but something is not right as it's not working:
string sqlQuery = "Select * from atable";
OracleConnection data = new OracleConnection("ConnectionString");
data.Open();
OracleCommand oc = new OracleCommand(sqlQuery, data);
OracleDataAdapter oa = new OracleDataAdapter();
oa.SelectCommand = oc;
Sorry I missunderstood.
ddl1.DataSource = dv.Table
ddl1.DataTextField = "A";
ddl1.DataValueField = "A";
ddl1.DataBind();
ddl1.DataSource = dv.Table
ddl1.DataTextField = "A";
ddl1.DataValueField = "A";
ddl1.DataBind();
ASKER
ok, but the populating of dataview code does not seem correct. Can you give the feedback on that?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Since my function relys on a dataview how do fill the data view (not data set)?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ok this is what I have now, in terms of populating the dataview, and it's not working:
string connectionstring = "Provider=msdaora;Data Source=somedata;User Id=a;Password=a;";
string sqlQuery = "Select * from atable order by b";
OracleConnection data = new OracleConnection(connectionstring);
data.Open();
OracleCommand oc = new OracleCommand(sqlQuery, data);
OracleDataAdapter oa = new OracleDataAdapter();
DataSet ds = new DataSet();
oa.Fill(ds);
DataView GroupView = new DataView(ds.Tables[0]);
ddl1.DataSource = GroupView;
ddl1.DataTextField = "A";
ddl1.DataValueField = "A";
ddl1.DataBind();
Can you give me the error message or behaviour ?
ASKER
The ddl is not populated with the values. No error message.
This line doesn't look right: DataView GroupView = new DataView(ds.Tables[0]);
what does passing ds.Tables[0] do but pass the first index/value to view, right?
This line doesn't look right: DataView GroupView = new DataView(ds.Tables[0]);
what does passing ds.Tables[0] do but pass the first index/value to view, right?
A dataset can contain many tables. Your SQL command returns only one table without any relations, so when we need to get datas from the dataset, we only have to get the first and only table, that's why I used the index 0.
What you forgot is to assign the select command to the adapter :
oa.SelectCommand = oc;
Otherwise the adapter just does nothing.
What you forgot is to assign the select command to the adapter :
oa.SelectCommand = oc;
Otherwise the adapter just does nothing.
ASKER
Ok added that but it's still not filling the ddl1 w/ values
Please post your code.
ASKER
I dropped the OracleCommand code as that is unnecessary. Here's the code as it stands:
private void BindDDLGroup()
{
DataView dv = new DataView();
string connectionstring = "data source=somedata;persist security info=False;user id=a;password=a;";
string sqlQuery = "Select * from atable order by b";
OracleConnection data = new OracleConnection(connectionstring);
data.Open();
OracleDataAdapter oa = new OracleDataAdapter(sqlQuery,data);
DataSet ds = new DataSet();
oa.Fill(ds);
GroupView = ds.Tables[0].DefaultView;
ddlGroup.Items.Clear();
string ListText = string.Empty;
for(int i=0; i<GroupView.Table.Rows.Count; i++)
{
ListText = GroupView[i]["Afield"].ToString();
ListItem item = new ListItem(ListText, GroupView[i]["Afield"].ToString());
ddlGroup.Items.Add(item);
}
ddlGroup.DataBind();
}
}
Ok every thing seems fine. Do you have datas in your table ?
ASKER
I finally found the issue. It wasn't this code that was the issue. VS2003 puts these 'silly' #regions in the code and adds InitializeComponents. That was all missing in this code. I added it and then worked like a charm. thank you so much for helping me!
Good to hear,
have fun !
have fun !
DataView.DataSource = dataset;
DataView.DataBind();
If it's not what you were looking for, detail you question a little more and it will be a pleasure to help you.
Luc