Link to home
Start Free TrialLog in
Avatar of Wade Knox
Wade KnoxFlag for United States of America

asked on

how to add a bound dropdownlist in asp.net formview insertitemtemplate using C#

I am using visual studio 2008 express

I am using an access database

I have a form view control which has a datasource of a "business object"

How do I make it so that the "company name" field in my webform has a dropdown list of company names to choose from when a user tries to add a new record?

The Company Name field is in the Company Table of the crm.mdb access DB

The ID is available on the page it is just hidden.

SEE PIC
pic-web.JPG
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Avatar of Wade Knox

ASKER

thanks for your quick answers but:

Please do not provide links to examples as i have explored many of the google results with no avail. I am looking for a straight forward answer for my specific situation.

Thanks
<asp:DropDownList ID="ddlCompany" runat="server" >
                            </asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection datacon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLink"].ConnectionString);
            datacon.Open();
            SqlCommand datacmd = new SqlCommand("usp_SelectAllDepartment",datacon);
            datacmd.CommandType = CommandType.StoredProcedure;
           
           
            DataSet ds = new DataSet();

            SqlDataAdapter adpt = new SqlDataAdapter(datacmd);
           
            adpt.Fill(ds);
                                   
            ddlCompany.DataSource = ds;
            ddlCompany.DataTextField = "CompanyName";
            ddlCompany.DataValueField = "CompanyId";
            ddlCompany.DataBind();
           
            datacon.Close();
        }


    }
      
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
        {
Populate1();
}
}

public void Populate1()
{

       
         OleDbConnection DBConn = default(OleDbConnection);
       
         OleDbDataAdapter DBCommand = default(OleDbDataAdapter);
       
         DataSet DSPageData = new DataSet();
       
         DBConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + "DATA SOURCE=" + Server.MapPath("db1.mdb;"));
       
         DBCommand = new OleDbDataAdapter("SELECT CompanyId, CompanyName From Company  Order By CompanyName", DBConn);
       
         DBCommand.Fill(DSPageData, "Company");
       
         DropDownList1.DataSource = DSPageData.Tables("Company").DefaultView;
       
           
         DropDownList1.DataBind();
       
     }
}
I think the link which I've provided gives the straight comment on the problem that you are facing.
roshmon the example you provided is using VB and I need a c# solution.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Roshmon I dont need a standard list items solution I need it to be bound to a table in my access DB.

Thanks for your effort and sorry bfor being so confusing.
I guess I need to insert and record creation date to be automatically generated also.
any other suggestions are greatly appreciated!