Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

7
Fetching Clicked Item 1 CTEC2902 Advanced Programming Using list boxes to fetch specific records

Transcript of Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Page 1: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 1

CTEC2902Advanced Programming

Using list boxes to fetch

specific records

Page 2: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 2

Viewing The Data

Populating a list box from UKCities table

Assume table definition to beUKCities (ID, City, County, Population)

RecordRecordRecordRecordEtc.

Data Table

Also assume we want to1. Show City field2. Get ID field

lstCities.DataSource = dtCities;

lstCities.DataTextField = “City“;

lstCities.DataValueField = “ID”;

lstCities.DataBind();

Page 3: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 3

Viewing The Data

On screen,clicked item ishighlighted

In the background,

1.zero-based index value of clicked item is

placed in SelectedIndex property

2.the clicked item itself is placed in SelectedItem

property

3.the corresponding value field data is placed in

SelectedValue property

What happens when you click on an item?

Page 4: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 4

clsSQLServer DB = new clsSQLServer(“MyDB.mdf”);

DataTable dtEmps = new DataTable;

DB.Execute("sp_tblStaff_GetAll");dtEmps = DB.QueryResults;

lstSurnames.DataSource = dtEmps;lstSurnames.DataTextField = “Surname“ lstSurnames.DataValueField = “AccNo“; lstSurnames.DataBind();

Viewing The Data

In a data-bound list box

You can now say things like …txtAccountNumber.Text = lstSurnames.SelectedValue;

Corresponding valueis in this property

Page 5: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 5

Viewing The Data

In a data-bound list box

long PKey;

PKey = lstSurnames.SelectedValue;

Dim SQL As StringSQL = "SELECT * FROM employees “ & _ “WHERE ID=“ & PKey

clsSQLServer DB = new clsSQLServer(“MyDB.mdf”)

DataTable dtEmps = new DataTable;DB.Execute("sp_tblStaff_GetAll");dtEmps = DB.QueryResults;

lstSurnames.DataSource = dtEmps;lstSurnames.DataTextField = “Surname“; lstSurnames.DataValueField = “AccNo“; lstSurnames.DataBind();

You can now executean SQL like =

Page 6: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 6

‘Create your database and data table objects‘Populate your data table

‘Set data source property to data table namelstCities.DataSource = dtCities;

‘Set display fieldlstCities.DataTextField = “City”;

'Set the value field; i.e. the data that will be ‘available when a city is clicked onlstCities.DataValueField = “ContactName“;lstCities.DataBind();

Viewing The Data

Getting Data Out of a data-bound list box

Another example

When you click a city name, the corresponding ContactNamewill be automatically placed in SelectedValue property

string TheName;

TheName = lstCities.SelectedValue;

You can now use TheName as a parameter in your SQL SELECT command

Q: how to run a stored procedure with parameters?

Page 7: Fetching Clicked Item1 CTEC2902 Advanced Programming Using list boxes to fetch specific records.

Fetching Clicked Item 77

Viewing The Data

Summary

RecordRecordRecordRecordEtc.

Data TableUse DataTextField to say whichfield value is to be displayed

Use DataValueField to say whichfield value will be in SelectedValue

When an item is clicked,SelectedValue contains value offield specified by DataValueField

SelectedIndex returns index ofclicked item (zero-based)

SelectedItem represents the completerecord, as DataRecordView, so you NEED TO use .ToString

Items listed in same orderas the records in data table;i.e. use ORDER BY in SELECT