Discussion Locked

ASP.Net Databind in DropDownList

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I tried to display the values from oracle table into a dropdownlist which is in asp.net.I used the below code to bind the data.When I debug,the page loads completely.But it doesn't show anything in the dropdownlist.How can I get the Oracle table values in asp.net dropdownlist.
I used the below code:

private void Page_Load(object sender, System.EventArgs e)
{
InitializeComponent();
ConnStr="Provider=msdaora;Data Source=inchnred;User Id=inchnset;Password=inchn;";
Conn=new OleDbConnection(ConnStr);
Conn.Open();
DataAdpt=new OleDbDataAdapter("SELECT * FROM TBLTEST",Conn);
DataAdpt.SelectCommand.CommandType=CommandType.Text;
DataSet ds=new System.Data.DataSet("TBLTEST");
DataAdpt.Fill(ds,"tbltest");
DropDownList1=new DropDownList();
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="Name";
DropDownList1.DataValueField="EMP";
DropDownList1.DataBind();
}

*Table name is TBLTEST
*It has columns Name,EMP

7 replies

by nzbn

If it works in debug mode but not when you run it it must be something to do with how your code finds the database. Maybe ODBC not setup properly - if the page loads fine its not an ASP problem its your SQL Connection string. Try the www.asp.net forum dedicated to oracle connections.
Also try using the gui wizard to connect you to the database - ie drag and drop using the RAD tool menu and follow the wizard (must be in gui not code mode) when you do this. Then go to the code and see what you get and compare the two.

by vrvaperanki

I had the same problem while I was trying to connect in VB.Net. I tried to change the provider and I got it. U can try it also. Type provider=oraoledb.oracle
This should work. Also check data source, UserID and password for correct spelling

by bsmgopal1984

Poster rated this answer.
Thanks for your ideas.
Now I got clearly....
I used the below code:

if(!IsPostBack)
{
ConnStr="Provider=msdaora;Data Source=inchnset;User Id=inchnasset;Password=inchnqw;";
Conn=new OleDbConnection(ConnStr);
cmd=new OleDbCommand("select deptmgr,deptno from tblmgr",Conn);
Conn.Open();
Dropdownlist3.DataSource=cmd.ExecuteReader();
Dropdownlist3.DataTextField="DEPTMGR";
Dropdownlist3.DataValueField="DEPTNO";
Dropdownlist3.DataBind();
Conn.Close();
}

by henry888chin

If you haven't created the "tbltest" table in the "dsTest" dataset, you'll need to create one. However, "tbltest" needs to be defined to have the fields as in the actual TBLTEST and/or other tables in the database. It seems that ADO.NET does not know how to put the retrieved table data in the current "TBLTEST" dataset.

The way I'd do is to define the "tbltest" table similar to the same table or a set of tables in the database for the fields that I need to work with. For example, I'll create a "tblPay" within a "dsPayroll". "tblPay" will contain only fields that I need to work with. When selecting fields from the actual database, do a "select a.ssn, a.last_name, a.first_name, b.pay_rate, c.days_worked from Personnel a, PayRate b, Timecard c". Once you have the "tblPay" defined, the retrieved data will know where to go in the memory database. Hopefully, this will help to point you in the right direction. Regards, HC.

This discussion is closed to new replies.