Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Having difficulty loading the drop down list box with data from the first record in the table.

What am I doing wrong ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;



namespace EDAIF
{
public partial class Loanmaster : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Page Load Begins
if (!IsPostBack)
{
try
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
connect.Open();
SqlDataAdapter da = new SqlDataAdapter(@"SELECT [ID_CODE] ,
[CUSTOMER] ,
[APPRS_NO] ,
[REG_CERT] ,
[CURRENT_STATUS]
FROM LOANMASTER ", connect);


DataSet ds = new DataSet();
da.Fill(ds, "LOANJOIN");

ddl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0][" CURRENT_STATUS "].ToString();

txt_Id_Code.Text = ds.Tables["LOANJOIN"].Rows[0]["ID_CODE"].ToString();
txt_Mail1.Text = ds.Tables["LOANJOIN"].Rows[0]["MAIL1"].ToString();

lblstatus.Text = "First record successful";
connect.Close();
GridView1.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
txt_Id_Code.Enabled = false;
txt_Appraisal.Enabled = false;
txt_Customer.Focus();

}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}

}


//Load End
Posted
Updated 4-Jun-14 16:18pm
v2
Comments
Member 10744248 4-Jun-14 23:03pm    
Not Clear.

The first record field is 'NON-ACTIVE' which was not picked
Member 10744248 4-Jun-14 23:05pm    
That has been done
DamithSL 4-Jun-14 23:29pm    
check my updated answer

In the Page_Init event, you have to init your drop down list with data read from the database, like in the next example:
C#
protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        try
        {
            //
            // Init the center drop down list used as filter control
            //
            List<Group> centerList = DataContext.Groups.ToList<Group>();
            SortedDictionary<string, int> sortedData = new SortedDictionary<string, int>();
            _groupDropDownList.DataSource = sortedData;
            _groupDropDownList.DataValueField = "value";
            _groupDropDownList.DataTextField = "key";
            _groupDropDownList.DataBind();
            //
            _groupDropDownList.Items.Insert(0, new ListItem("All Groups", "0"));
            //
            _groupDropDownList.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            RaGridViewEventLog.LogException(ex);
            this.ErrorMessage = "Error in loading the data from the database!";
        }
    }
}
 
Share this answer
 
Comments
Member 10744248 5-Jun-14 1:58am    
Where is the Page_Init ?
You are setting selected value of the status ddl. But before that you need to bind the drop down with some items. then only you can set one of item as selected base on the item value.

and also note that you have spaces at the beginning and at the end of your column name
change
C#
dl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0][" CURRENT_STATUS "].ToString();

to
C#
dl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0]["CURRENT_STATUS"].ToString();
 
Share this answer
 
v2

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