drop down menu selected w/ source code

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

Currently, I can add values the drop down menu list inside the grid
view.
I want my value from SQL database to be select in the drop down menu
list (COLOR).
Also, once the user change to the value, update it back to database.

How can I achieve this? Thanks a lot.


select * from name;
+----+--------+-------+
| ID | OWNER | COLOR |
+----+--------+-------+
| 1 | Jim | red |
| 2 | Andrea | green |
| 3 | Dan | white |
| 4 | Tim | blue |
+----+--------+-------+

Below are working code to display values from database and generic
drop down menu list.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"; >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;<asp:Label ID="Label1" runat="server" Text="This is a
test!"></asp:Label>

</div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="OWNER" HeaderText="OWNER" /

<asp:BoundField DataField="COLOR"
HeaderText="COLOR_VIEW" />
<asp:TemplateField>
<itemtemplate>
<asp:DropDownList id="colorDropList"
runat="server" AutoPostBack="true"
OnSelectedIndexChanged="colorDropList_SelectedIndexChanged">
</asp:DropDownList>
</itemtemplate>

</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>

Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


if (!(Page.IsPostBack))
{

// SQL query
string sqlStr = @"
SELECT ID, OWNER, COLOR
FROM name;
";

string connInfo = "Data Source=xxx;Initial
Catalog=fruits;User ID=xxx;Password=xxx";
SqlConnection sqlConn = new SqlConnection(connInfo);
SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlConn);

try
{
sqlConn.Open();
SqlDataAdapter sqlData = new SqlDataAdapter();

sqlData.SelectCommand = sqlCmd;
DataTable dataTbl = new DataTable();
sqlData.Fill(dataTbl);
GridView1.DataSource = dataTbl;
GridView1.DataBind();

DropDownList dropdownlist = new DropDownList();
foreach (GridViewRow row in GridView1.Rows)
{
dropdownlist =
((DropDownList)row.FindControl("colorDropList"));

string occur_list =
"red,white,blue,green,purple,black";

string[] colorFreq = occur_list.Split(',');

for (int i = 0; i < colorFreq.Length; i++)
{
dropdownlist.DataValueField = colorFreq[i];
dropdownlist.Items.Add(colorFreq[i]);
}
}

}//try
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
sqlConn.Dispose();
}

}


}



protected void colorDropList_SelectedIndexChanged(object sender,
EventArgs e)
{


}


}
.



Relevant Pages

  • Re: Access2k upsizing to SQL2k Automation
    ... Once I manually set the primary keys on two of the ... correctly against the SQL database. ... Upsize Wizard only marks some of the columns 'null'. ...
    (microsoft.public.access.conversion)
  • Re: too much OOP ?
    ... SQL database, ... You cannot say that without specifying what kind of requests are planned. ... nothing about inventory management, but in my area of interest relational ...
    (comp.object)
  • Re: Update Sql Server 2000 database from the HP3000
    ... collects those updates via ODBC every 10 or so seconds. ... essentially update the SQL database at the same time as the main ... Authorised and regulated by the Financial Services Authority. ...
    (comp.sys.hp.mpe)
  • Re: Q: Datatables, Datasets and updating
    ... I have two tables A and B in the SQL database. ... ID_A (the primary key of A) and NAME_A (a string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Configuration & Deployment issues
    ... > to logged in a SQL Server database. ... > moment this is Windows Server 2000 and Windows XP. ... > even take the new upgrades. ... We can use our SQL database to see if the ...
    (microsoft.public.windowsxp.configuration_manage)