Click here to Skip to main content
15,881,875 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I'm having problem filling the dropdownlist with data from database in a windows based application.
I know how to do that in ASP.NET, but I can't find a solution in .NET 4.5 windows based.
The following is the code in ASP.NET
C#
public void Paraqit_ddListKlasa()
        {
            DataTable dt = new DataTable();
            SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
            sqlConn.Open();

            string SQLstringu = "Select  KlasaViti, ID_Klasa from KLASA ";
            SqlCommand cmd = new SqlCommand(SQLstringu, sqlConn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            ddListKlasa.DataSource = dt;
            ddListKlasa.DataTextField = "KlasaViti";
            ddListKlasa.DataValueField = "ID_Klasa";
            ddListKlasa.DataBind();
            ddListKlasa.Items.Insert(0, new ListItem("--Zgjedh--", "0"));
            sqlConn.Close();
        }


In pageload I gave the following code:
C#
if (!IsPostBack)
{

    Paraqit_ddListKlasa();

}


How to fix this and make it work in windows based application.
Thank you in advance for your reply.
Posted
Comments
Rahul Kumar 16-Jun-14 5:10am    
IsPostBack in Windows Application ?

call Paraqit_ddListKlasa() after InitializeComponent(); in form constructor method.

for ex:

C#
public MyForm()
   {
       InitializeComponent();
       coutries = new List<Country>();
       OUtility = new Utility();
       coutries = OUtility.GetCoutries();
       cbCountry.DataSource = coutries;
       cbCountry.DisplayMember = "name";
       cbCountry.ValueMember = "code";
   }
// My Method
    public List<country> GetCoutries()
        {
            countryList = new List<country>();            
            string query = "SELECT Id, Code, Name FROM country";
            dataTable = objSqlUtility.ExceuteAdapter(query);
            foreach (var item in dataTable.AsEnumerable())
            {
                objCountry = new Country(Convert.ToInt32(item[0]), item[1].ToString(), item[2].ToString());
                countryList.Add(objCountry);
            }
            return countryList;
        }
 
Share this answer
 
v2
Same as asp.net You can use Form_load Event to call any event.


for quick reference visit
http://www.daveoncsharp.com/2009/11/binding-a-windows-forms-combobox-in-csharp/[^]
 
Share this answer
 
first remove
C#
if (!IsPostBack)

there is not post back in windows application.

then call your dropdownlist/combobox populate method at Form_Load() event. for an example...


C#
private void Form1_Load(object sender, System.EventArgs e)
    {
Paraqit_ddListKlasa();
    }
 
Share this answer
 
you found if (!IsPostBack) in Windows application. strange...

for binding combobox in windows application you can use like this:-
C#
using (SqlConnection cnn = new SqlConnection(connString))
{
    cnn.Open();
    using (SqlDataAdapter sda = new SqlDataAdapater("Select  KlasaViti, ID_Klasa from KLASA", cnn))
    {
        DataTable dt = new DataTable();
        sda.Fill(dt);

        comboBox.ValueMember = "ID";
        comboBox.DisplayMember = "KlasaViti";
        comboBox.DataSource = dt;
    }
}
 
Share this answer
 
in winform use ComboBox Control and also you need to use DisplayMember and ValueMember properties. call below method in form load event ( don't check for IsPostBack, it is not available in winform)
No need to call DataBind method like in web application
C#
public void Paraqit_ddListKlasa()
{
    DataTable dt = new DataTable();
    SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
    sqlConn.Open();

    string SQLstringu = "Select  KlasaViti, ID_Klasa from KLASA ";
    SqlCommand cmd = new SqlCommand(SQLstringu, sqlConn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    DataRow dataRow = dt.NewRow();
    dt["KlasaViti"] = "--Zgjedh--";
    dt.Rows.InsertAt(dataRow, 0);
    comboBox1.DataSource = dt;
    comboBox1.DisplayMember= "KlasaViti";
    comboBox1.ValueMember  = "ID_Klasa";

    sqlConn.Close();
}
 
Share this answer
 
v2
Comments
dr_iton 16-Jun-14 6:55am    
I'm getting an error on following row.
dt["KlasaViti"] = "--Zgjedh--";

How to solve that, and after this in the asp.net the code was that after checking the value in ddl1 it populated the ddl2 with values that was related with ddl1 ID or value.
Hello dr_iton,

There is no need of this code :
C#
if (!IsPostBack)
          {
              Paraqit_ddListKlasa();
          }



In windows base application you need to write code in form_load event.

C#
public void Paraqit_ddListKlasa()
        {
            DataTable dt = new DataTable();
            SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
            sqlConn.Open();
 
            string SQLstringu = "Select  KlasaViti, ID_Klasa from KLASA ";
            SqlCommand cmd = new SqlCommand(SQLstringu, sqlConn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
 
            ddListKlasa.DataSource = dt;
            ddListKlasa.DataTextField = "KlasaViti";
            ddListKlasa.DataValueField = "ID_Klasa";
            ddListKlasa.DataBind();
            ddListKlasa.Items.Insert(0, new ListItem("--Zgjedh--", "0"));
            sqlConn.Close();
        }



call your method in form_load event like this :
C#
Paraqit_ddListKlasa();



Other Idea:
-----------

1. Always try to create DAL (Data Access Layer) that you can use in WEB and WINDOWS FORM application as well.

Let me know if you have any further query.
Please, go through above link.
If its your answer, please mark as solution.
Best of luck for hunting solutions.

Thank you.
Regards,

Manoj Kalla
 
Share this answer
 
v2
Comments
Thanks7872 16-Jun-14 3:36am    
There is no link in your above solution. Which link are you talking about in all your solutions?
Hello friend, first of all there is no PostBack concept in Windows Application. Please remove the check from the following code:
C#
//This check is meaningless in Windows App
if (!IsPostBack)
{

}

Let me know if you have any other query.

- DD
 
Share this answer
 
Comments
Thanks7872 16-Jun-14 3:21am    
Any other query is his question itself. Don't post content which is simply not an answer to the qestion.
Debabrata_Das 16-Jun-14 3:30am    
Hi Rohan, Are you asking me anything? Sorry I didn't get your post.
- DD
Thanks7872 16-Jun-14 3:32am    
How this answer can help solve OP's issue? Explain it and then it can be called Solution. In its current form, it doesn't seem like solution to the problem.
Debabrata_Das 16-Jun-14 4:00am    
Rohan, I've gone through the code changes shared in the question and I didn't find any issue except the part where it's checking whether a Page is postback. As per my knowledge, the check is irrelevant hence suggested to remove the check and try.

Please let me know if you feel otherwise.

- DD

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900