Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox databind

Status
Not open for further replies.

plsh

Programmer
Mar 5, 2003
118
ZA
Hi there,

I am trying to fill a combobox with values from a sql server 2000 database. Unfortunately I come from VB6 and normal straight asp and my boss wants me to magically learn how to code in C#. I have been trying to figure it out and not much luck, can't seem to find any decent examples, can someone point me in the right direction or post some sort of example please?

TIA
 
I guess filling the combo with values from db is pretty simple. No matter which platform u r from.

i am putting one example here... i hope it wud help u

in aspx page:
<asp:DropDownList Runat="server" ID="cboPageTitle" DataTextField="PM_Name" DataValueField="PM_ID" />


In code behind:
Private Sub LoadPageTitleCombo()
Try
'stores dataview of pages
Dim dvPages As DataView
'store datatable of pages
Dim dtPages As DataTable
'constant to store
Const SP_PAGE_MASTER_LIST = "spPage_Master_List"

'Stores Connection object
Dim objConnection As SqlConnection
'initialization of connection object
objConnection = New SqlConnection()

'stores command object
Dim objCommand As New SqlCommand()
'setting command object's properties
objCommand.CommandType = CommandType.StoredProcedure
objCommand.CommandText = SP_PAGE_MASTER_LIST

'Assign the connection string to the connection object.
objConnection.ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings("ConnectionString"))

'Open the connection
objConnection.Open()

'associate connection with command
objCommand.Connection = objConnection

'adapter object
Dim objDataAdapter As SqlDataAdapter
'Execute the query
objDataAdapter = New SqlDataAdapter(objCommand)
'initializating dataview
dvPages = New DataView()
'initializing datatable
dtPages = New DataTable()
'get the pages
objDataAdapter.Fill(dtPages)

'Close connection object
objConnection.Close()

'Set nothing in the connection object
objConnection = Nothing
'get the list of pages
dvPages = dtPages.DefaultView
'bind the dataview with combo
cboPageTitle.DataSource = dvPages
cboPageTitle.DataBind()
'insert default selected value
cboPageTitle.Items.Insert(0, New ListItem("-- Select --", "0"))

Catch objException As Exception
mstrErrorMessage = objException.Message
End Try
End Sub



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'if page is loaded for first time
If Not Page.IsPostBack Then
'load page titles in combo
LoadPageTitleCombo()
End If
End Sub

Rakhi
 
Another Example...


public void Page_Load(object sender, System.EventArgs e) {
if (!Page.IsPostBack) {
LoadEx();
}
}

public void LoadEx() {
try {
DataSet ds = new DataSet();
ds = sb.getEx(txtID.Text);
if (ds.Tables[0].DefaultView.Count > 0){
ddlEx.DataSource = ds.Tables[0].DefaultView;
ddlEx.DataBind();
}
}
catch (Exception ex) {
string strErr;
strErr = syslblError.Text + " A problem was found loading the Drop Down List. Please try again.";
syslblError.Text = strErr.Trim();
syslblErrorMessage.Text = ex.Message;
}
}

public DataSet getEx(int intParam1) {
DataSet ds = new DataSet();

try {
string strSQL;
strSQL = "SELECT * FROM dbo.tblEx WHERE (dbo.tblEx_ID = @intParam1)";

conn = new SqlConnection(strConn);
SqlCommand cmd;
cmd = new SqlCommand (strSQL, conn);
cmd.Parameters.Add("@intParam1", SqlDbType.Int).Value = intParam1;

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "Returned");
}
catch {
}
finally {
conn.Close();
}

return ds;
}


Hope this helps...

Hope everyone is having a great day!

Thanks - Jennifer
 
Hi jzelhart,

Thank you but I cannot follow your code. What are sb and ddlEx? What are they refering to?

Thanks
 
ddlEx is the name of the DropDownList Control

sb is a variable that refers to the class that the SQL Connection and Dataset are created in.

HTML
<asp:dropdownlist id="ddlEx" runat="server" Font-Size="12px" Font-Names="Arial" Font-Bold="True" Width="100%" DataTextField="tblExDesc_VC" DataValueField="tblEx_ID"> </asp:dropdownlist>

Class Variable
DataAccess.SQLBean sb = new DataAccess.SQLBean();

Class Called beginning code...

namespace DataAccess
{
public class ksSQLBean {
string strConn = "server=SERVER;uid=LOGIN;pwd=PASSWORD;Initial Catalog=tblEx";
SqlConnection conn;

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top