You must Sign In to post a response.
  • Category: ASP.NET

    DropDown in 3tier Architecture

    Hello friends,
    I am working on a project in asp.net using 3 tier architecture where i am making a form say city where i enter the city name and after saving it gets saved in database.Now i have the same city label but with a dropdown list control on another form .I need to get the same city name which are already there in database in my dropdown list. How will I achieve ?

    Expecting your guidance and help.
  • #762563
    Hi

    As you are already storing first dropdown list values in to database, you can bind the second dropdown list from database values.

    protected void Page_Load(object sender, EventArgs e)
    {
    bindlist2();
    }

    public void bindlist2()
    {
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText = " Select * From tablename"";
    cmd.Connection = con;
    con.Open();
    DataTable dt = new DataTable();

    dt.Load(cmd.ExecuteReader());
    con.Close();

    DropDownList2.DataSource = dt;
    DropDownList2.DataTextField = "Name";
    DropDownList2.DataValueField = "ID";
    DropDownList2.DataBind();
    }

    Regards

    Sai Krishna Reddy.

  • #762565
    hello,
    the first thing is that i am using only one dropdownlist,the city name which i need to save is a textbox control
    the other thing i am using stored procedure and i am implementing it using 3 tier architecture

  • #762566
    Hello Rohit,

    You can refer the below code :

    string cs = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
    SqlCommand sqlcmd = new SqlCommand("select * from Exam", con);
    con.Open();
    ddlExam.DataSource = sqlcmd.ExecuteReader();
    ddlExam.DataTextField = "ExamNo";
    ddlExam.DataValueField = "ExamName";
    ddlExam.DataBind();
    con.Close();
    }


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762570
    Hello Rohit,

    Refer the below code using stored procedure :

    SqlConnection conn = new SqlConnection(connStr);

    conn.Open();

    SqlDataAdapter dAd = new SqlDataAdapter("Select City FROM Employee", conn);

    dAd.SelectCommand.CommandType = CommandType.StoredProcedure;

    DataSet dSet = new DataSet();

    try
    {
    dAd.Fill(dSet, "EmployeeTable");
    MyDropDownList.DataSource = dSet;
    MyDropDownList.DataBind();
    }
    catch(Exception ex)
    {
    throw ex;
    }
    finally
    {
    dSet.Dispose();
    dAd.Dispose();
    conn.Close();
    }

    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762572
    Hi

    You can follow this steps for your issue

    Step1


    Create Table TblCity
    (
    CityId int primary key identity(1,1),
    Cityname varchar(40)
    )

    Create Proc sp_InsertCity
    @cityname varchar(50)
    as
    Insert into TblCity values(@cityname)


    Step2

    Client Side code


    <asp:TextBox ID="TxtCity" runat="server" ></asp:TextBox>
    <asp:Button ID="bt1" runat="server" Text="Submit" OnClick="bt1_Click" />

    <asp:DropDownList ID="drp1" runat="server"></asp:DropDownList>


    Server Side C#



    protected void bt1_Click(object sender, EventArgs e)
    {
    string connectionString = "Data Source=PCNAME;Initial Catalog=DBNAME;Integrated Security=True;";
    using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand command = new SqlCommand("sp_InsertCity", con))
    {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@cityname", TxtCity.Text);
    con.Open();
    command.ExecuteNonQuery();
    BindCitys();
    }
    }
    }

    private void BindCitys()
    {
    string connectionString = "Data Source=PCNAME;Initial Catalog=DBNAME;Integrated Security=True;";
    DataTable dt = new DataTable();
    SqlDataAdapter sqladp;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
    sqladp = new SqlDataAdapter("Select * from TblCity", con);
    sqladp.Fill(dt);
    drp1.DataSource = dt;
    drp1.DataTextField = "Cityname";
    drp1.DataValueField = "CityId";
    drp1.DataBind();
    }

    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762573
    Hi Rohit,

    Where you get struck, What you have tried so far..?

    If you want to get database result and display in dropdownlist control bind it in the below way,


    DataSet ds=//your data from Database;

    ddl1.DataSource=ds;
    ddl1.DataTextField="YourTextField";
    ddl1.DataValueField="YourValueField";
    ddl1.DataBind();


    If you struck in any place then post that part code.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762575
    The above can i use it for 3tier
    will u help me by making me clear the code for BAL,DAL

    thanks in advance

  • #762576
    Hi Rohit,

    Refer below link "dotnetspider.com/resources/45057-Working-with-3Tier-Application-with-Examples.aspx", here I explain you how to create 3-tier architecture with small example, may be this will help you to clear your doubts.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762577

  • #762580
    Hello,
    MY Problem----
    as i mentioned that i have a form where i enter the city name in textbox and after save event data gets saved in database.
    Data is saved in a table say tbl_city.The same data i need to have in my dropdown which is on another form say on registration form.
    Current Situation----
    Data gets saved in tbl_city.It is available in dataset but not in dropdown.So how could i bind data feom dataset to dropdown .

    Plzz help me and thanks a lot for all who have guided me

  • #762581
    Hello Rohit,

    We have provided you the possible ways. Why are not you trying our ways that we have provided with the sample code.

    Also with stored procedure i provided you the sample code. You just try to use and execute it.

    Than only you will know how to use it.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762609
    Hi Rohit.

    As you said you have data in dataset, use below code to get cities data from tbl_city to dropdownlist.

    using System.Web.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    SqlConnection cn = new SqlConnection();
    cn.ConnectionString = WebConfigurationManager.AppSettings["constr"];
    cn.Open();
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("select * from tbl_city", cn);
    da.Fill(ds, "p");
    DropDownList1.DataTextField = ds.Tables[0].Columns["Name"].ToString();
    DropDownList1.DataSource = ds.Tables[0];
    DropDownList1.DataBind();
    }
    }

    You can see the attached snapshot of cities from tbl_city binded to dropdownlist.

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment

  • #762616
    hello,
    thanks a lot all for your valuable advice and guidance..I got my problem solved.
    Now i have stuck again in a situation where i need to generate user id and password randomly after successful registration of user.
    The problem is after registration, user should get a pop up displaying user id and password where userid should be in the format (15008001 where 15=current year,008=city id,0001 is serialwise number which should increment one after the other)

    Is it possible to do in backend ?
    If yes plzz help ..

  • #762617
    Hi

    What is your issue?

    Can you share with me.

    Explain more need then only we reply fastly in your issue.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762618

  • #762619
    Jaykumar Sir,

    After registration of a particular user he/she should get a message displaying user id and password. Both should be generated auto. UserId should be in the format 15008001[where 15=current year,008=city id,0001 is serial wise number which should increment one after the other]

    How to achieve this?

  • #762628
    Hello

    The solution you got for the thread you posted. Now again why you are asking different requirement in the same thread of dropdown.

    Select the best answer from above responses which solved your dropdown issue.

    Post new thread with your requirement.

    Sridhar Thota.
    Editor: DNS Forum.


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.