The DropDownList is not retrieving values from database.
I want to set selected value of DropDownList in GridView row.
Can u please help me?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList txtdepname = (e.Row.FindControl("txtdepname") as DropDownList);
string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\sivaranjani\\Documents\\Database6.accdb";
OleDbConnection db = new OleDbConnection(str);
db.Open();
string st = "select DEPNAME,DID from tab";
OleDbCommand dbc = new OleDbCommand(st, db);
OleDbDataReader read = dbc.ExecuteReader();
txtdepname.DataSource = read;
txtdepname.DataTextField = "DEPNAME";
txtdepname.DataValueField = "DID";
txtdepname.DataBind();
read.Close();
db.Close();
}
}
protected void ShowData()
{
OleDbConnection connn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sivaranjani\Documents\Database6.accdb");
connn.Open();
OleDbCommand cmd = new OleDbCommand("Select [emp].[ID],[emp].[EMPID],[emp].[EMPNAME],[emp].[AGE],[tab].[DEPNAME] from emp INNER JOIN tab ON [emp].[DID] = [tab].[DID] ", connn);
OleDbDataReader rd = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rd);
connn.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GridView1.DataSource = dt;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}