Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
m using this Method to automatically fill all textbox and dropdown from database value
which is happening but in this case of dropdownlist it is not working.
what i can do to solve it?


private void paybillname()
        {
            string CS = ConfigurationManager.ConnectionStrings["SocietyCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spmemberpaybillautofill", con);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter Nameparameter = new SqlParameter("@MemberName", txtmembername.Text);
                cmd.Parameters.Add(Nameparameter);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                   
                   here at time of binding these dropdown how i bind this control in this way ?
                   // ddlsocietyname.DataTextField = rdr.GetValue(2).ToString();
                   //ddlsocietyname.DataValueField = rdr.GetValue(1).ToString();
                    
                    // dropdownlist control is not working
                    ddlsocietyname.SelectedValue = rdr.GetValue(2).ToString();
                    txtsocietyname.Text = rdr.GetValue(2).ToString();
                    txtMemberID.Text = rdr.GetValue(24).ToString();
                    txtmembername.Text = rdr.GetValue(26).ToString();
                    ddlmembername.SelectedValue = rdr.GetValue(26).ToString();
                    txtmemberRoomNumber.Text = rdr.GetValue(28).ToString();
                    txtcurrentmonth.Text = rdr.GetValue(33).ToString();
                    txtcidcocharges.Text = rdr.GetValue(13).ToString();
                }
                rdr.Close();
            }
        }</pre>
Posted

Hi Tarun,

I use a different method. Please see below if that helps;

C#
using System.Data;
using System.Data.SqlClient;
/****************/


            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adap = new SqlDataAdapter();
            DataTable dt = new DataTable();

            con.ConnectionString = ""; //your connection string goes here
            con.Open();

            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ""; //your stored procedure name goes here

            cmd.Parameters.AddWithValue("ParamName1", "ParamValue1");
            cmd.Parameters.AddWithValue("ParamName2", "ParamValue2");
            cmd.Parameters.AddWithValue("ParamName3", "ParamValue3");
            //....
            cmd.CommandTimeout = 10000;

            adap.SelectCommand = cmd;
            adap.Fill(dt); //Fill the data table with values obtained by executing the stored procedure

            con.Close();

            //bind your drop down here
            this.comboBox1.DataSource = dt;
            this.comboBox1.DisplayMember = "FieldToDisplayAsTextItemInComboBox"; //This is the field name whose values will be displayed as text items
            this.comboBox1.ValueMember = "FieldToBeUsedAsValueItem"; //This is the field name whose values will be used as values corresponding to relevant text items 

            //Fill your textboxes as below
            this.txtbox1.text = dt.Rows[0]["FieldName"].ToString();
            //...


Hope this helps.
Please mark the answer if appropriate.

Happy coding..!!

--Nayan
 
Share this answer
 
ddlmembername.SelectedValue = ddlmembername.Items.FindByValue(rdr.GetValue(26).ToString() ).Value;
 
Share this answer
 

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