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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectedIndex Value from Bound Control

Status
Not open for further replies.

karth23

Programmer
May 23, 2002
13
US
I have 3 Tables
Data Structure Below
Table1:
ADDRESS_ID INT ( Seed ) Primary Key
ST_ID INT Foriegn Key
CNT_ID INT Foriegn Key
First_NM NVARCHAR(50)

Table 2:
ST_ID INT Primary Key
ST_CD INT
ST_NM NVARCHAR(50)

Table3:
CNT_ID INT Primary Key
CNT_NM VARCHAR(50)

I have created 3 DataAdapter one Dataset
Default.aspx file has a datagrid, textbox controls and Update and Add Buttons.
I have one DropDownList Control (Bind Control) for display State abrivation.

i..e ST_CD from Table2
If the User select and a row from the Datagrid the details will display on appropriate text boxes
and dropdownList box.

My problem is I couldn't get the ID value i.e ST_ID from the Selected Dropdownlist when the user
click the Add button. How could I get the ID Value from the DropdownListbox

My dropdownlistbox name is ddlState
I am trying to get the ST_ID using the following code

i.e dr("ST_D") = ddlState.SelectedItem.Value

The above code is working fine when I user click Update button

The following code working fine for updation.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

SqlCnn.Open()

'Table1: - Address_Lst
DAAddressUSA.FillSchema(DsAddressBook1, SchemaType.Mapped, "Address_Lst")
DAAddressUSA.Fill(DsAddressBook1, "Address_Lst")

'Table3: - Country_Lst
DACountry.FillSchema(DsAddressBook1, SchemaType.Mapped, "Country_Lst")
DACountry.Fill(DsAddressBook1, "Country_Lst")

'Table2: - State_USA
DAState.FillSchema(DsAddressBook1, SchemaType.Mapped, "State_USA")
DAState.Fill(DsAddressBook1, "State_USA")

Session.Add("dsAddressUSA", DsAddressBook1)
dgUSA.DataBind()

End If

End Sub

Private Sub UpdateRow()
DsAddressBook1 = Session("dsAddressUSA")

Dim dt As DataTable = DsAddressBook1.Tables("Address_Lst")
Dim dr As DataRow()

'Get the seleced row using Address_Id
dr = dt.Select("Address_ID ='" + lblID.Text + "'")

dr(0)("FIRST_NM") = txtFirstName.Text.Trim
dr(0)("ST_ID") = ddlState.SelectedItem.Value

DAAddressUSA.Update(dr)

Session.Add("dsAddressUSA", DsAddressBook1)
UpdateRecords()
End Sub

Private Sub UpdateRecords()
DsAddressBook1 = Session("dsAddressUSA")
'dbUSA - DataGrid
dgUSA.DataBind()
End Sub

The Following Code not working

Private Sub InsertRow()

DsAddressBook1 = Session("dsAddressUSA")
Dim dt As DataTable = DsAddressBook1.Tables("Address_Lst")
Dim dr As DataRow = DsAddressBook1.ADDRESS_LST.NewRow

dr("FIRST_NM") = txtFirstName.Text.Trim
dr("ST_ID") = ddlState.SelectedItem.Value

DsAddressBook1.ADDRESS_LST.ADDRESS_IDColumn.AutoIncrementSeed = True
DsAddressBook1.ADDRESS_LST.AddADDRESS_LSTRow(dr)
DAAddressUSA.UpdateCommand = Me.SqlInsertCommand1()
DAAddressUSA.Update(DsAddressBook1)
Session.Add("dsAddressUSA", DsAddressBook1)
UpdateRecords()
End Sub

The above Insert return the following error
"Object reference not set to an instance of an object. "
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Could any one help me to resolve this error.

Karthik Bala
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top