unable to get selected value of listbox control when clicking button?

J

Joey

Hi There,

I am trying to get the selected value of a listbox when I click a button,
everything works ok and I can bind the list and when I have a basic page and
click a button to invoke a sub it works fine, but when I try to place it in
the sub I want to use it give me an eror saying that the "Object reference
not set to an instance of an object." I can't understand why it works in a
small sub in the same class but then when I try to use it in another sub it
falls over!



Joey



--- code in page

<script language="vb" runat="server">
Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
If Not Page.IsPostBack Then
lst.DataSource = CommonFunctions.BindStates
lst.DataValueField = "LocationID"
lst.DataTextField = "Location"
lst.Items.Insert(0, new ListItem("By State","0"))
lst.SelectedIndex = 0
lst.DataBind()
End If
End Sub
</script>

<asp:listbox id="lst" runat="server" Rows="1" AutoPostBack="false"
DataTextField="Location" DataValueField="LocationID"/>

<asp:TextBox id="txtlocationselection" width="250" maxlength= "250"
runat="server" />

<asp:button id="btnSubmit1" text="place listing" onClick="but_Click"
runat="server" />
</form>

---code behind sub

Sub Insertlisting_Click(Sender As Object, E As EventArgs)

--!suspectcode here

Dim curItem As String = lstS.SelectedItem.Value





'strlstRegion = trim(lstS.SelectedItem.Value.ToString())

'response.write (strlstRegion)



strManufacturer = txtManufacturer.text

strModelDescription = txtModelDescription.text

strModelDescription1 = txtModelDescription1.text

strtxtComments = txtComments.text

ccurtxtPrice = txtPrice.text

strtxt1 = txt1.text

strtxt2 = txt2.text

strtxt3 = txt3.text



If chk1.checked = true then

strChk1 = CHECKED

Else

strChk1 = NOTCHECKED

end if

If chk2.checked = true then

strChk2 = CHECKED

Else

strChk2 = NOTCHECKED

end if

If chk3.checked = true then

strChk3 = CHECKED

Else

strChk3 = NOTCHECKED

end if

If chk4.checked = true then

strChk4 = CHECKED

Else

strChk4 = NOTCHECKED

end if

If chk5.checked = true then

strChk5 = CHECKED

Else

strChk5 = NOTCHECKED

end if

If chk6.checked = true then

strChk6 = CHECKED

Else

strChk6 = NOTCHECKED

end if

If chk7.checked = true then

strChk7 = CHECKED

Else

strChk7 = NOTCHECKED

end if

If chk8.checked = true then

strChk8 = CHECKED

Else

strChk8 = NOTCHECKED

end if

If chk9.checked = true then

strChk9 = CHECKED

Else

strChk9 = NOTCHECKED

end if

If chk10.checked = true then

strChk10 = CHECKED

Else

strChk10 = NOTCHECKED

end if

'to do

'validate numeric fields

'change the size of the fields depending on input size


Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))


Try


myConnection.Open()


Dim myCommand As New SqlCommand("ListingAddControlValves", myConnection)

myCommand.CommandType = CommandType.StoredProcedure

'name

Dim pFormname As New SqlParameter("@formname", SqlDbType.Nchar, 200)

pFormname.Value = "Control Valves"

myCommand.Parameters.Add(pFormname)

'formid

Dim pFormid As New SqlParameter("@formid", SqlDbType.int)

pFormid.Value = "6"

myCommand.Parameters.Add(pFormid)

'directive

myCommand.Parameters.Add("@directive", SqlDbType.Nchar, 5).Value = "I"

'current

myCommand.Parameters.Add("@currentlisting", SqlDbType.int).Value = 0

'txtmanufacturer

Dim pManufacturer As New SqlParameter("@manufacturer", SqlDbType.Nchar, 200)

pManufacturer.Value = strManufacturer

myCommand.Parameters.Add(pManufacturer)

'model

Dim modeldesc As New SqlParameter("@model", SqlDbType.Nchar, 300)

modeldesc.Value = strModelDescription

myCommand.Parameters.Add(modeldesc)

'desc

Dim desc As New SqlParameter("@description", SqlDbType.Varchar, 1000)

desc.Value = strModelDescription1

myCommand.Parameters.Add(desc)

'Nominal

Dim ptxt1 As New SqlParameter("@frm1value", SqlDbType.char, 20)

ptxt1.Value = strtxt1

myCommand.Parameters.Add(ptxt1)

'Pressure Sustaining

Dim pchk1 As New SqlParameter("@frm2value", SqlDbType.char, 10)

pchk1.Value = strChk1

myCommand.Parameters.Add(pchk1)

'Pressure Reducing

Dim pchk2 As New SqlParameter("@frm3value", SqlDbType.char, 10)

pchk2.Value = strChk2

myCommand.Parameters.Add(pchk2)

'Presure Relief

Dim pchk3 As New SqlParameter("@frm4value", SqlDbType.char, 10)

pchk3.Value = strChk3

myCommand.Parameters.Add(pchk3)

'Serge Anticipation

Dim pchk4 As New SqlParameter("@frm5value", SqlDbType.char, 10)

pchk4.Value = strChk4

myCommand.Parameters.Add(pchk4)

'Electronic Control Valves

Dim pchk5 As New SqlParameter("@frm6value", SqlDbType.char, 10)

pchk5.Value = strChk5

myCommand.Parameters.Add(pchk5)

'Air Release

Dim pchk6 As New SqlParameter("@frm7value", SqlDbType.char, 10)

pchk6.Value = strChk6

myCommand.Parameters.Add(pchk6)

'Water Meters

Dim pchk7 As New SqlParameter("@frm8value", SqlDbType.char, 10)

pchk7.Value = strChk7

myCommand.Parameters.Add(pchk7)

'other

Dim ptxt2 As New SqlParameter("@frm9value", SqlDbType.char, 20)

ptxt2.Value = strtxt2

myCommand.Parameters.Add(ptxt2)

'Bronze

Dim pchk8 As New SqlParameter("@frm10value", SqlDbType.char, 10)

pchk8.Value = strChk8

myCommand.Parameters.Add(pchk8)

'Cast Iron

Dim pchk9 As New SqlParameter("@frm11value", SqlDbType.char, 10)

pchk9.Value = strChk9

myCommand.Parameters.Add(pchk9)

'plastic

Dim pchk10 As New SqlParameter("@frm12value", SqlDbType.char, 10)

pchk10.Value = strChk10

myCommand.Parameters.Add(pchk10)

'quantity

Dim ptxt3 As New SqlParameter("@quantity", SqlDbType.Int, 4)

ptxt3.Value = strtxt3

myCommand.Parameters.Add(ptxt3)

'comments

Dim ptxtcomments As New SqlParameter("@comments", SqlDbType.Nchar, 200)

ptxtcomments.Value = strtxtComments

myCommand.Parameters.Add(ptxtcomments)

'listing price

Dim pccurtxtPrice As New SqlParameter("@listprice", SqlDbType.Money, 4)

pccurtxtPrice.Value = ccurtxtPrice

myCommand.Parameters.Add(pccurtxtPrice)

'location

Dim pstrlstRegion As New SqlParameter("@location", SqlDbType.Int,4)

pstrlstRegion.Value = curItem

myCommand.Parameters.Add(pstrlstRegion)

'userid

Dim puserid As New SqlParameter("@userid", SqlDbType.Int,4)

puserid.Value = Session("MemberID")

myCommand.Parameters.Add(puserid)


Dim pUsername As New SqlParameter("@Username", SqlDbType.Nchar, 200)

pUsername.Value = Session("UserName")

myCommand.Parameters.Add(pUsername)


myCommand.Parameters.Add("@status", SqlDbType.char, 50).Value = "Pending
Payment"

myCommand.Parameters.Add(New SqlParameter("@listingID", SqlDbType.Int))

myCommand.Parameters("@listingID").Direction() = ParameterDirection.Output



myCommand.ExecuteReader()


dim inlast as Integer

inlast = myCommand.Parameters("@listingID").value

response.redirect("uploadimage.aspx?id=" & Server.UrlEncode(inlast)&"&D="&
Server.UrlEncode("3"))


Catch ex as InvalidCastException

Status.Text = ex.ToString()

Catch ex As SqlException

Status.Text = "Database error: " & ex.message

Catch ex As Exception

Status.Text = "General error: " & ex.message

End Try

'End If

End Sub
 
K

Kevin Spencer

"Object reference not set to an instance of an object" is one of the easiest
exceptions to track down, esp. if you know what line of code the exception
was thrown at. It means that the code is referencing an object that doesn't
exist (not initialized, removed, etc). All you have to do is identify the
object references in the statement, and figure out which one doesn't exist.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Joey

Hi Kevin,

I do know the line where the error is happening "> > Dim curItem As String =
lstS.SelectedItem.Value" the thing is if I create a small button_click()
function and then run the same code in the same class using the same control
it works, but if I try to use the function that I'm using it falls over and
I just can't work out why?

Any other ideas ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top