Assuming that you are using MySql as your database,lets say you have a combobox which values need to be obtained from the database.Something like below;
For this tutorial,i have the following table and its structure in the database. The name of my table is sys_country.
I will retrieve both CountryId, and CountryName and set CountryName as the visible items and the the CountryId as the value.
Step 1:
Add aspxComboBox to your aspx page either by manually typing it or dragging the controls from the toolbox.Its should be somewhere under DX.14.2 : Common Controls (my devEx version us 14.2.6).
Set the ID, and add the following properties to the tag.
- ValueField = Your object which later to be defined in your object class, as you can see from above, i set it as countryId, which is my object for the CountryId from my db. The countryId will be the value field for the selected items. Ex : when Malaysia is selected, it will return 1 as the value.
- TextField = The text to be displayed in the combobox.
-ValueType = The type of the value which in this tutorial, the countryId type which is Int.
-OnInit = In devExpress, it is a good approach to bind your controls in the OnInit event rather then binding it at page_load.You can read more about it at the devExpress page.
Step 2
Create a new class in your project that will store your objects and will be called by your code behind to get the list of countries set in the data object. This will serve as the Business Logic layer. In this tutorial i named it as CountryObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace myDevExBlog
{
public class CountryObject
{
//objects
private int _countryId;
private string _countryName;
public int countryId
{
get { return _countryId; }
set { _countryId = value; }
}
public string countryName
{
get { return _countryName; }
set { _countryName = value; }
}
public CountryObject()
{
}
public List<CountryObject> getCountries()
{
//to store the list of countries
List<CountryObject> lstCountries = new List<CountryObject>();
//to call the mysqlscripts
mysqlScripts clsMysqlScripts = new mysqlScripts();
DataTable dt = clsMysqlScripts.retrieveCountries();
foreach(DataRow dRow in dt.Rows)
{
//set data from datatable to objects
CountryObject oCountry = new CountryObject();
oCountry.countryId = Convert.ToInt32(dRow["CountryId"]);
oCountry.countryName = dRow["CountryName"].ToString();
lstCountries.Add(oCountry);
}
//retunr the list
return lstCountries;
}
}
}
Step 3
Create another new class in your project that will handle your mysql scripts. This will serve as the Data Access Layer and will return datables.In this tutorial i named it as mysqlscripts.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace myDevExBlog
{
public class mysqlScripts
{
public DataTable retrieveCountries()
{
String mysqlConnStr = ConfigurationManager.ConnectionStrings["SWCS"].ConnectionString;
MySqlConnection mysqlDbcon = new MySqlConnection(mysqlConnStr);
String mysqlQuery = "SELECT CountryId, CountryName FROM sys_country";
MySqlCommand mysqlComm = new MySqlCommand(mysqlQuery, mysqlDbcon);
MySqlDataAdapter mysqlDA = new MySqlDataAdapter(mysqlComm);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
mysqlDbcon.Open();
mysqlDA.SelectCommand = mysqlComm;
mysqlDA.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception ex)
{
mysqlDbcon.Close();
}
finally
{
mysqlDbcon.Close();
}
return dt;
}
}
}
Step 4
Now,for the last part, you can navigate back to your aspx code behind, and handle the OnInit event that was created earlier in Step 1:
protected void cbCountry_Init(object sender, EventArgs e)
{
CountryObject clsCountryObject = new CountryObject ();
List<CountryObject > lstOfCountries = new List<CountryObject >();
lstOfCountries = clsCountryObject.getCountries();
cbCountry.DataSource = lstOfCountries;
cbCountry.DataBind();
}
Build and Run the project and you are good to go! :)
No comments:
Post a Comment