Object reference not set to an instance of an object?

J

Jerome

I keep getting this error!? It seems like the code can't find the control?

Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String = ConfigurationSettings.AppSettings("conString")

Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER
JOIN dbo.tRubriques ON dbo.tVisites.IDRubrique =
dbo.tRubriques.IDRubrique ORDER BY NomRubrique,
dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 
M

Mark Fitzpatrick

You have to test for the fact that the value may be null, or that the use
may not have selected anything in the listbox.
If the user has not selected anything in the listbox you can test this by
comparing the SelectedIndex property of the listbox. If the selected index
is less than zero (a -1) then there is no selected item.

You may just want to also give a quick perusal of the code to ensure that
the values are getting set correctly and that each item in the list actually
is generating a value in HTML.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
J

Jerome

Thank you, but I HAVE selected an item in the list before pushing the
button (which triggers the click event)! So it should not be null??

Or do I have to check anyway even if there's an item selected?

Eliyahu said:
Likely, there is no item selected. You can first check SelectedItemIndex
on -1 before trying accessing SelectedItem.

Eliyahu

I keep getting this error!? It seems like the code can't find the control?

Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String =
ConfigurationSettings.AppSettings("conString")

Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER
JOIN dbo.tRubriques ON dbo.tVisites.IDRubrique =
dbo.tRubriques.IDRubrique ORDER BY NomRubrique,
dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 
J

Jerome

Thank you, but I HAVE selected an item in the list before pushing the
button (which triggers the click event)! So it should not be null??

Or do I have to check anyway even if there's an item selected?

Mark said:
You have to test for the fact that the value may be null, or that the use
may not have selected anything in the listbox.
If the user has not selected anything in the listbox you can test this by
comparing the SelectedIndex property of the listbox. If the selected index
is less than zero (a -1) then there is no selected item.

You may just want to also give a quick perusal of the code to ensure that
the values are getting set correctly and that each item in the list actually
is generating a value in HTML.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

I keep getting this error!? It seems like the code can't find the control?

Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String =
ConfigurationSettings.AppSettings("conString")

Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER JOIN
dbo.tRubriques ON dbo.tVisites.IDRubrique = dbo.tRubriques.IDRubrique
ORDER BY NomRubrique, dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 
J

Jerome

How can I check this?
And isn't ViewState enabled by default?

Eliyahu said:
Is ViewState enabled for the page?

Eliyahu

Thank you, but I HAVE selected an item in the list before pushing the
button (which triggers the click event)! So it should not be null??

Or do I have to check anyway even if there's an item selected?

Eliyahu said:
Likely, there is no item selected. You can first check SelectedItemIndex
on -1 before trying accessing SelectedItem.

Eliyahu



I keep getting this error!? It seems like the code can't find the
control?
Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String =

ConfigurationSettings.AppSettings("conString")


Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER
JOIN dbo.tRubriques ON dbo.tVisites.IDRubrique =
dbo.tRubriques.IDRubrique ORDER BY NomRubrique,
dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 
E

Eliyahu Goldin

Likely, there is no item selected. You can first check SelectedItemIndex
on -1 before trying accessing SelectedItem.

Eliyahu
 
E

Eliyahu Goldin

Is ViewState enabled for the page?

Eliyahu

Jerome said:
Thank you, but I HAVE selected an item in the list before pushing the
button (which triggers the click event)! So it should not be null??

Or do I have to check anyway even if there's an item selected?

Eliyahu said:
Likely, there is no item selected. You can first check SelectedItemIndex
on -1 before trying accessing SelectedItem.

Eliyahu

I keep getting this error!? It seems like the code can't find the control?

Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String =
ConfigurationSettings.AppSettings("conString")

Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER
JOIN dbo.tRubriques ON dbo.tVisites.IDRubrique =
dbo.tRubriques.IDRubrique ORDER BY NomRubrique,
dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 
E

Eliyahu Goldin

It is enabled by default. Make sure there is EnableViewState="false" in the
page directive in the .aspx file.

Eliyahu

Jerome said:
How can I check this?
And isn't ViewState enabled by default?

Eliyahu said:
Is ViewState enabled for the page?

Eliyahu

Thank you, but I HAVE selected an item in the list before pushing the
button (which triggers the click event)! So it should not be null??

Or do I have to check anyway even if there's an item selected?

Eliyahu Goldin wrote:

Likely, there is no item selected. You can first check SelectedItemIndex
on -1 before trying accessing SelectedItem.

Eliyahu



I keep getting this error!? It seems like the code can't find the
control?

Here's my code:
---------------------------
<%@ Page Language="VB" ContentType="text/html" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Dim strConString as String =

ConfigurationSettings.AppSettings("conString")


Sub Page_Load
If Not IsPostBack then

Dim conRubriques as SQLConnection
Dim cmdSelect as SQLCommand
Dim dtrRubriques as SqlDataReader

conRubriques= New SqlConnection(strConString)
conRubriques.Open()
cmdSelect= New SqlCommand("SELECT dbo.tVisites.IDRubrique,
dbo.tVisites.VisiteOrdre, dbo.tVisites.VisiteNom, [NomRubrique] + ': ' +
[VisiteNom] AS Visite, dbo.tVisites.Internet FROM dbo.tVisites INNER
JOIN dbo.tRubriques ON dbo.tVisites.IDRubrique =
dbo.tRubriques.IDRubrique ORDER BY NomRubrique,
dbo.tVisites.VisiteOrdre", conRubriques)
dtrRubriques = cmdSelect.ExecuteReader()
lstVisite1.DataSource= dtrRubriques
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataTextField = "Visite"
lstVisite1.DataBind()
dtrRubriques.Close()
conRubriques.Close()

End If
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

dim cnOVS as sqlconnection
dim strInsert as string
dim cmdInsert as sqlcommand
dim strVis as string

cnOVS= new sqlconnection(strConString)
strInsert= "Insert tInscrip (Visites) values (@Visites)"
cmdInsert=new sqlcommand (strInsert, cnOVS)
strVis = lstVisite1.SelectedItem.Value
cmdInsert.parameters.add("@Visites", strVis)
cnOVS.open()
cmdInsert.ExecuteNonQuery()
cnOVS.close()

End Sub

</script>
<html>
<body>
<p>Online Umeldung fir Visiten vum Service Educatif</p>
<form name="form1" runat="server">
<p>1. Visit: <asp:ListBox ID="lstVisite1" Runat="server" />
<asp:Button ID="Button1" Text="enregistrer" runat="server"
OnClick="button1_Click" /></p>
</form>
</body>
</html>
--------------------------------------------
And this line causes the error: strVis = lstVisite1.SelectedItem.Value

Anyone have any idea what the problem could be??

Thanks a lot!
 

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

Top