473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Web Part drop down list

Hi All,

I'm trying to create a web part that will populate data in a drop down menu, I'm not sure how to pass the data from the method where I'm retrieving the data to the CreateChildControls method. It compiles fine, but when I add the web part to the page, there is no data in the drop down menu. I'm new on the whole .net stuff. Any help will be greatly appreciate. Thanks in avance

here is my code I have so far

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace TelecomWebParts
{
public class Vendors : WebPart
{
DropDownList ddVendors;
SqlConnection myConnection;
SqlCommand cmdVendor;
SqlDataReader drVendor;


public void getRecords()
{
myConnection = new SqlConnection(@"Server=servername;user id=user;password=passwor;Database=databasename");
myConnection.Open();
cmdVendor = myConnection.CreateCommand();
cmdVendor.CommandText = "Select VendorID, CompanyName from tblVendor";
drVendor = cmdVendor.ExecuteReader();
//ddVendors = new DropDownList();
//ddVendors.DataSource = drVendor;
//ddVendors.DataTextField = "CompanyName";
//ddVendors.DataValueField = "VendorID";
//drVendor.Close();
//myConnection.Close();
}

protected override void CreateChildControls()
{
ddVendors = new DropDownList();
ddVendors.ID = "vendors";
ddVendors.AppendDataBoundItems = true;
ddVendors.DataSource = drVendor;
ddVendors.DataTextField = "CompanyName";
ddVendors.DataValueField = "VendorID";
ddVendors.DataBind();
drVendor.Close();
this.Controls.Add(ddVendors);
}

}
}
Mar 3 '08 #1
1 1478
Hi again,

I was able to do it, but when I try to add the web part to a page, I got the following error:
An unhandled exception has occurred.
Request for the permission of type "System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToke=b77a5c56e089" failed.

Any idea?
Thanks
here is the code....

public DataTable getRecords()
{
SqlConnection myConnection;
myConnection =
new SqlConnection(@"Server=servernam;user id=username;password=password;Database=databasenam e");
SqlDataAdapter da = new SqlDataAdapter("Select VendorID, CompanyName from tblVendor", myConnection);
DataTable dt = new DataTable("Vendor");
da.Fill(dt);
myConnection.Close();
return dt;
}
protected override void CreateChildControls()
{
DropDownList ddVendors;
ddVendors =
new DropDownList();
ddVendors.ID =
"vendors";
//ddVendors.AppendDataBoundItems = true;
ddVendors.DataSource = getRecords();
ddVendors.DataTextField =
"CompanyName";
ddVendors.DataValueField =
"VendorID";
ddVendors.DataBind();
this.Controls.Add(ddVendors);
}

Hi All,

I'm trying to create a web part that will populate data in a drop down menu, I'm not sure how to pass the data from the method where I'm retrieving the data to the CreateChildControls method. It compiles fine, but when I add the web part to the page, there is no data in the drop down menu. I'm new on the whole .net stuff. Any help will be greatly appreciate. Thanks in avance

here is my code I have so far

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace TelecomWebParts
{
public class Vendors : WebPart
{
DropDownList ddVendors;
SqlConnection myConnection;
SqlCommand cmdVendor;
SqlDataReader drVendor;


public void getRecords()
{
myConnection = new SqlConnection(@"Server=servername;user id=user;password=passwor;Database=databasename");
myConnection.Open();
cmdVendor = myConnection.CreateCommand();
cmdVendor.CommandText = "Select VendorID, CompanyName from tblVendor";
drVendor = cmdVendor.ExecuteReader();
//ddVendors = new DropDownList();
//ddVendors.DataSource = drVendor;
//ddVendors.DataTextField = "CompanyName";
//ddVendors.DataValueField = "VendorID";
//drVendor.Close();
//myConnection.Close();
}

protected override void CreateChildControls()
{
ddVendors = new DropDownList();
ddVendors.ID = "vendors";
ddVendors.AppendDataBoundItems = true;
ddVendors.DataSource = drVendor;
ddVendors.DataTextField = "CompanyName";
ddVendors.DataValueField = "VendorID";
ddVendors.DataBind();
drVendor.Close();
this.Controls.Add(ddVendors);
}

}
}
Mar 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Paul | last post by:
Just wondering if there is a way to set up hyperlinks in a drop down list, so each item in the dropdown list navigates to a different page.
3
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
3
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
1
by: pmelanso | last post by:
Hello, I have a drop down list which is dynatically loaded from a database and I have a second drop down list that is also dynatically loaded depending on what is selected in the first drop down...
10
by: lorirobn | last post by:
Hi, I have a form with several combo boxes, continuous form format, with record source a query off an Item Table. The fields are Category, Subcategory, and Color. I am displaying descriptions,...
8
by: Ed Dror | last post by:
Hi there ASP.NET 2.0 VB & SQL Express Lest take Northwind Categories Products as example I create a table that hold these two together and I create a stored procedure like select ProductID,...
5
by: ashok893 | last post by:
I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.