Link to home
Create AccountLog in
Avatar of newbieal
newbiealFlag for United States of America

asked on

fill DataView in code (vs2003, c#)

How do I fill a DataView with data in code?
Avatar of OptimusSupernova
OptimusSupernova
Flag of Canada image

Well let's assume you are using a dataset you would do it this way :

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
Avatar of newbieal

ASKER

DataView does not have DataSource as an option.


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"];
        } 

Open in new window

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;

Open in new window

Sorry I missunderstood.

ddl1.DataSource = dv.Table
ddl1.DataTextField = "A";
ddl1.DataValueField = "A";
ddl1.DataBind();
ok, but the populating of dataview code does not seem correct.  Can you give the feedback on that?
SOLUTION
Avatar of OptimusSupernova
OptimusSupernova
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Since my function relys on a dataview how do fill the data view (not data set)?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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();

Open in new window

Can you give me the error message or behaviour ?
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?

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.
Ok added that but it's still not filling the ddl1 w/ values
Please post your code.
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();
 
			
		}
 
	
		
	}

Open in new window

Ok every thing seems fine. Do you have datas in your table ?
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 !