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!
 
J

Jerome

My .aspx page contains the following now:

<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" Debug="true" EnableViewState="false" %>

But I keep getting the same error message...?


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

Eliyahu

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!
 
J

Jerome

Ok, I've done what you've suggested:
-----------------------
if lstVisite1.SelectedIndex <0 then
exit sub
else
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 If
------------------------

And the way the page reacts now it's sure that it thinks that NO item
was selected! Though I HAVE selected one ...

Any ideas??

Thanks.


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

Ok, but I've still got the same problem then. The code reacts as if
there was NO item selected though I HAVE selected one!?
------------------------
<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" Debug="true" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">

Sub Page_Load

If Not IsPostBack then

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

dim cn as SQLConnection
dim cmd as SQLCommand
dim ds as DataSet
dim ad as SqlDataAdapter

cn = new SqlConnection()
cn.ConnectionString = strConString
cmd = 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")
cn.Open()
cmd.Connection = cn
ds = new DataSet()
ad = new SqlDataAdapter(cmd)
ad.Fill(ds)
lstVisite1.DataSource = ds
lstVisite1.DataTextField = "Visite"
lstVisite1.DataValueField = "VisiteNom"
lstVisite1.DataBind()

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

if lstVisite1.SelectedIndex >-1 then
label1.text=lstVisite1.SelectedItem.text
else
label1.text="no item selected"
End If

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" />
<asp:Label ID="Label1" runat="server" />
</p>
</form>
</body>
</html>
----------------------------
???


Eliyahu said:
Oh sorry, I meant there is no EnableViewState="false". It shouldn't be
there.

Eliyahu

My .aspx page contains the following now:

<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" Debug="true" EnableViewState="false" %>

But I keep getting the same error message...?


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

Eliyahu



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

Eliyahu Goldin wrote:


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>
lstVisite1.SelectedItem.Value
Anyone have any idea what the problem could be??

Thanks a lot!
 
E

Eliyahu Goldin

Oh sorry, I meant there is no EnableViewState="false". It shouldn't be
there.

Eliyahu

Jerome said:
My .aspx page contains the following now:

<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" Debug="true" EnableViewState="false" %>

But I keep getting the same error message...?


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

Eliyahu

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

Eliyahu Goldin wrote:

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top