Thursday, March 29, 2012
Object reference
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
I get an error in line:
If Session("user") Is Nothing Then...
What could be wrong?Apparently, the Session doesn't exist in the context of the line you posted.
--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"simon" <simon.zupan@.stud-moderna.si> wrote in message
news:e4bE4IDtDHA.2148@.TK2MSFTNGP12.phx.gbl...
> I get an error message:
> Exception Details: System.NullReferenceException: Object reference not set
> to an instance of an object.
> I get an error in line:
> If Session("user") Is Nothing Then...
> What could be wrong?
>
>
Hi,
> I get an error in line:
> If Session("user") Is Nothing Then...
> What could be wrong?
Session object is null (Nothing).
1) Check <sessionState> element in your Web.config file.
2) Make sure that you retrieve it correctly.
--
______________________________
With best wishes, Arthur Nesterovsky
Please visit my home page:
http://www.nesterovsky-bros.com
Hi,
If I use session object in user control(aspx page), it doesn't work.
If I write the same code in default.aspx then everything is fine.
In past I have code and all my functions in include.asp page, which I
included on all my pages.
I had line:
If Session("user") Is Nothing Then
response.redirect "login.asp"
End If
Now I move all my functions to user control (by the book) and register it on
all my pages - approximate the similar like with include.
But if the session won't work, I don't want to write line with session
checking on all my pages.
Should I use include statement(which is past from asp) or is there some
other way?
Thank you,
Simon
"Arthur Nesterovsky" <arthur_NO_SPAM_PLEASE_@.nesterovsky-bros.com> wrote in
message news:uHYr6XDtDHA.1876@.TK2MSFTNGP09.phx.gbl...
> Hi,
> > I get an error in line:
> > If Session("user") Is Nothing Then...
> > What could be wrong?
> Session object is null (Nothing).
> 1) Check <sessionState> element in your Web.config file.
> 2) Make sure that you retrieve it correctly.
> --
> ______________________________
> With best wishes, Arthur Nesterovsky
> Please visit my home page:
> http://www.nesterovsky-bros.com
Hi,
> I had line:
> If Session("user") Is Nothing Then
> response.redirect "login.asp"
> End If
> Now I move all my functions to user control (by the book) and register it
on
> all my pages - approximate the similar like with include.
Try to check
HttpContext.Current.Session("user") instead.
--
______________________________
With best wishes, Arthur Nesterovsky
Please visit my home page:
http://www.nesterovsky-bros.com
I have control leviMenu.ascx, where I define session:
If HttpContext.Current.Session("user") Is Nothing Then
Dim uporabnik(2)
uporabnik(0) = "1"
uporabnik(1) = "Simon Zupan"
uporabnik(2) = "2"
Session("user") = uporabnik
End If
Then I register it on the page:
<%@. Register TagPrefix ="Menu" TagName = "LeviMenu" Src =
"userControls\leviMenu.ascx"%
then use it on page:
<menu:leviMenu runat="server" ID="Levimenu1"></menu:leviMenu
Then on the same page in Private Sub Page_Load event, I'm reading the value
from session:
HttpContext.Current.Session("user")(0)
And I gett an error message:
System.NullReferenceException: Object variable or With block variable not
set.
The session object is not recognized.
How can I solve this problem?
I have session object in user control because it's registered on all pages.
I can write the session code on each page separeted, it works than, but than
I don't have the code on one place, it doesn't make any sence.
Thank you,
Simon
"Arthur Nesterovsky" <arthur_NO_SPAM_PLEASE_@.nesterovsky-bros.com> wrote in
message news:Oz3PxfMtDHA.1756@.TK2MSFTNGP09.phx.gbl...
> Hi,
> > I had line:
> > If Session("user") Is Nothing Then
> > response.redirect "login.asp"
> > End If
> > Now I move all my functions to user control (by the book) and register
it
> on
> > all my pages - approximate the similar like with include.
> Try to check
> HttpContext.Current.Session("user") instead.
> --
> ______________________________
> With best wishes, Arthur Nesterovsky
> Please visit my home page:
> http://www.nesterovsky-bros.com
Object reference error
Dim sourcePane As String = CType(sender, ImageButton).Attributes("sourcepane")
Dim targetPane As String = CType(sender, ImageButton).Attributes("targetpane")
Dim sourceBox As ListBox = CType(Page.FindControl(sourcePane), ListBox)
Dim targetBox As ListBox = CType(Page.FindControl(targetPane), ListBox)
end sub
why i can not find my control with the "Page.FindControl" method?
the string name is correcte, the control exist on my page. I still have that error "object reference not set to an instance of..object"
what do i do wrong here !!
thanxYou haven't provided enough information for us to answer your question. Whether Page.FindControl will actually find a control depends on where the control is on your page.
To see what I mean - and hopefully to allow you to solve your own problem - please read:
In Search Of Controls
As an aside, you should always test whether an object has a value before you start working with it.
For example, you should do something like:
' Find the target pane
Dim FoundTargetPane As Control
FoundTargetPane = Page.FindControl( targetPane )' If found, cast the target pane to a ListBox control
Dim targetBox As Listbox
If Not FoundTargetPane Is Nothing Then
targetBox = CType( FoundTargetPane, ListBox )
End If' Work with the targetBox
If Not targetBox Is Nothing Then
' we can work with targetBox, knowing that's we've found it
End If
Monday, March 26, 2012
Object reference not set to an instance
I'm getting
'Object reference not set to an instance of an object' error when I have
Dim lblEnterNew As System.Web.UI.WebControls.LabelIf DataGrid1.EditItemIndex > -1 Then
lblEnterNew.Visible = False
Else
lblEnterNew.Visible = True
End If
and my control in the page is
<asp:Label id="lblEnterNew" runat="server" class="dept"></asp:Label>
Any idea why I'm getting this error?Are you sure that the error occurs on the label? The label looks ok to me.
You are getting the error because you are dim-ing an instance of a label in a variable at the same time it is probably already dimmed in the hidden code-behind in the '#Region " Web Form Designer Generated Code " ' region. You would probably not get the error if you dimmed it wit the New statement "Dim lblEnterNew as New System.Web.UI.WebControls.Label" but then you would have problems referencing the actual control on the web page.
Try removing the "Dim lblEnterNew as System.Web.UI.WebControls.Label" line from the code as this should fix the problem and there should be a statement similiar to: "Protected Withevents lblEnterNew as Label" near the top of the code behind page already.
That worked. Should all of my controls be dimmed in the '#Region' but not variables?
All controls which you create in the design mode of Visual Studio are automatically dimed in the #Region section. You can define your own variables just after the class has started!
object reference not set to an instance of an object
Hello,
I am trying to use the ObjectDataSource, & the class file where I'm getting the error is this:
using
System;using
System.Data;using
System.Configuration;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
System.Data.SqlClient;
//SQL Server Data Layer to Players table
public
classPlayerLogic{
public PlayerLogic(){
}
privatestaticstring cs =ConfigurationManager.ConnectionStrings["BBDBA"].ConnectionString;publicstaticDataSet GetPlayers(){
DataSet ds =newDataSet();SqlConnection cn =newSqlConnection(cs);cn.Open();
SqlDataAdapter da =newSqlDataAdapter("dbo.usp_PlayersGetAll", cn);da.Fill(ds);
cn.Close();
cn.Dispose();
return ds;}
publicstaticDataSet GetPlayer(int PlayerID){
DataSet ds =newDataSet();SqlConnection cn =newSqlConnection(cs);SqlCommand cmd =newSqlCommand("dbo.usp_PlayersGetOne", cn);cmd.CommandType =
CommandType.StoredProcedure;cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.PlayerID", PlayerID));cn.Open();
SqlDataAdapter da =newSqlDataAdapter(cmd);da.Fill(ds);
cn.Close();
cn.Dispose();
return ds;}
publicstaticstring SavePlayer(Player1 MyPlayer){
SqlConnection cn =newSqlConnection(cs);SqlCommand cmd =newSqlCommand("dbo.usp_PlayerSave2005", cn);string err ="";string rtn ="";if ((Convert.ToInt32(MyPlayer.Rank) > 25) || (Convert.ToInt32(MyPlayer.Rank) < 0)){
err =
"Rank must be between 0 & 25 ";}
if (MyPlayer.PlayerName ==String.Empty){
err =
"PlayerName required";}
if (err.Length == 0){
cmd.CommandType =
CommandType.StoredProcedure;cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.ErrMsg",SqlDbType.VarChar, 254));cmd.Parameters[
"@dotnet.itags.org.ErrMsg"].Direction =ParameterDirection.Output;cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.NewID",SqlDbType.Int));cmd.Parameters[
"@dotnet.itags.org.NewID"].Direction =ParameterDirection.Output;cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.PlayerID", MyPlayer.PlayerID));cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.PlayerName", MyPlayer.PlayerName));cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.Rank", MyPlayer.Rank));cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.HireDate", MyPlayer.HireDate));cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.PositionCode", MyPlayer.PositionCode));cmd.Parameters.Add(
newSqlParameter("@dotnet.itags.org.Salary", MyPlayer.Salary));cn.Open();
cmd.ExecuteNonQuery();
//err = cmd.Parameters["@dotnet.itags.org.ErrMsg"].Value.ToString();//rtn = cmd.Parameters["@dotnet.itags.org.NewID"].Value.ToString();}
//if (err.Length > 0)//{// rtn = "*" + err;//}//else//{// rtn = cmd.Parameters["@dotnet.itags.org.NewID"].Value.ToString();//}cn.Close();
cn.Dispose();
return rtn;}
publicstaticint DeletePlayer(int PlayerID){
SqlConnection cn =newSqlConnection(cs);SqlCommand cmd =newSqlCommand("dbo.usp_PlayerDelete", cn);int rtn;cmd.CommandType =
CommandType.StoredProcedure;cn.Open();
rtn = cmd.ExecuteNonQuery();
cn.Close();
cn.Dispose();
return rtn;}
}
The code where I'm calling the class from is:
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partialclassDefaultODS : System.Web.UI.Page{
protectedvoid Page_Load(object sender,EventArgs e){
lblMsg.Text =
"";}
protectedvoid frvPlayer_DataBound(object sender,EventArgs e){
//if (frvPlayer.CurrentMode == FormViewMode.Edit)//{// LinkButton btn = new LinkButton();// btn = (LinkButton)frvPlayer.FindControl("btnDelete");// btn.Attributes.Add("onClick", "return confirm('Confirm your delete!')");//}}
protectedvoid frvPlayer_ItemUpdated(object sender,FormViewUpdatedEventArgs e){
if (e.AffectedRows > 0){
grdPlayers.DataBind();
lblMsg.Text =
"PlayerID " + e.Keys["PlayerID"] +" " + e.NewValues["PlayerName"].ToString() +" Updated";e.KeepInEditMode =
false;}
else{
e.KeepInEditMode =
true;}
}
protectedvoid frvPlayer_ItemUpdating(object sender,FormViewUpdateEventArgs e){
lblMsg.Text =
"";if((Convert.ToInt32(e.NewValues["Rank"]) > 25) || ((Convert.ToInt32(e.NewValues["Rank"]) < 0))){
lblMsg.Text =
"Rank must be between 0 & 25 " +"; " + lblMsg.Text;e.Cancel =
true;}
if (e.NewValues["PlayerName"].ToString() ==String.Empty){
lblMsg.Text =
"Player name required";e.Cancel =
true;}
}
protectedvoid frvPlayer_ItemDeleted(object sender,FormViewDeletedEventArgs e)
{
if (e.AffectedRows > 0){
lblMsg.Text =
"PlayerID " + e.Keys["PlayerID"] +" " +e.Values[
"PlayerName"].ToString() +" Deleted";grdPlayers.DataBind();
frvPlayer.ChangeMode(
FormViewMode.ReadOnly);}
else{
lblMsg.Text =
"Error deleting record";}
grdPlayers.DataBind();
}
protectedvoid frvPlayer_ItemInserted(object sender,FormViewInsertedEventArgs e){
if (e.AffectedRows > 0){
lblMsg.Text = e.Values[
"PlayerName"].ToString() +" Inserted";grdPlayers.DataBind();
e.KeepInInsertMode =
false;}
else{
e.KeepInInsertMode =
true;}
}
protectedvoid grdPlayers_RowUpdated(object sender,GridViewUpdatedEventArgs e){
if (e.AffectedRows > 0){
grdPlayers.DataBind();
lblMsg.Text =
"PlayerID# " + e.Keys["PlayerID"].ToString() +"- " +"Updated";}
else{
lblMsg.Text =
"Error updating record";}
}
protectedvoid frvPlayer_ItemInserting(object sender,FormViewInsertEventArgs e){
lblMsg.Text =
"";if ((Convert.ToInt32(e.Values["Rank"]) < 0) || ((Convert.ToInt32(e.Values["Rank"]) > 25))){
lblMsg.Text =
"There can't be less than 0 players or more than 25";e.Cancel =
true;}
if (e.Values["PlayerName"].ToString() ==String.Empty){
lblMsg.Text =
"Player name required";e.Cancel =
true;}
}
//protected void frvPlayerDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)//{// //set the insert flag// e.Command.Parameters["@dotnet.itags.org.PlayerID"].Value = 0;//}//protected void frvPlayerDataSource_Updated(object sender, SqlDataSourceStatusEventArgs e)//{// if(e.Command.Parameters["@dotnet.itags.org.ErrMsg"].Value.ToString().Length > 0)// {// lblMsg.Text = "<font color=red>" + e.Command.Parameters["@dotnet.itags.org.ErrMsg"].Value.ToString() + "</Font>";// }//}//protected void frvPlayerDataSource_Inserted(object sender, SqlDataSourceStatusEventArgs e)//{// if (e.Command.Parameters["@dotnet.itags.org.ErrMsg"].Value.ToString().Length > 0)// {// lblMsg.Text = "<font color=red>" + e.Command.Parameters["@dotnet.itags.org.ErrMsg"].Value.ToString() + "</Font>";// }//}}
The error I get is this:
Server Error in '/DataSample10' Application.
Object reference not set to an instance of an object.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 18: }Line 19:Line 20: private static string cs = ConfigurationManager.ConnectionStrings["BBDBA"].ConnectionString;Line 21: Line 22: public static DataSet GetPlayers()
Source File:c:\BegASPNET2\C#\DataSample10\App_Code\PlayerLogic.cs Line:20
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] PlayerLogic..cctor() in c:\BegASPNET2\C#\DataSample10\App_Code\PlayerLogic.cs:20[TypeInitializationException: The type initializer for 'PlayerLogic' threw an exception.] PlayerLogic.GetPlayer(Int32 PlayerID) in c:\BegASPNET2\C#\DataSample10\App_Code\PlayerLogic.cs:53[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +72 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +358 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +676 System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2652 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +84 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99 System.Web.UI.WebControls.FormView.DataBind() +23 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91 System.Web.UI.WebControls.FormView.EnsureDataBound() +176 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +101 System.Web.UI.Control.EnsureChildControls() +134 System.Web.UI.Control.PreRenderRecursiveInternal() +109 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4435
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
I'm a big newbie, so I'd appreciate any input you guys have!
Thanks,
Orbit
no one wants to read that all...
Find the line thats erroring and post JUST that relevant code.
check what it ConfigurationManager.ConnectionStrings["BBDBA"] in debug mode.
As curt_c said please post the relevant code
Hi Curt,
Sorry it was so verbose. As a newbie, I just wanted to be sure I didn't leave anything out. Thank you for your reply, however. I would post a culled version of the question, but "vtrajan" resolved the issue for me.
Thanks again,
Orbit
Dear "vtrajan",
Thank you, your suggestion resolved my issue. Debugging is an area I need to improve upon bigtime. And I'll take the suggestion to be more succinct in my question posts from now on.
Thanks again,
Orbit
Object reference not set to an instance of an object
<%@dotnet.itags.org. Page Language="VB" Debug="True" CodePage="28599" %>
<%@dotnet.itags.org. Import NameSpace="System.Data" %>
<script runat="server">
Sub Page_Load(Obj As Object,E As EventArgs)
'DataSet Nesnesini Olustur -------
Dim ds as new DataSet("ornek_dataset")
'Tablo Olustur-----------
Dim musteritable as New DataTable("müsteriler")
'Tabloya Sutunlar Ekle--------
musteritable.Columns.Add ("ad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("soyad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("meslek",System.Type.GetType("System.String"))
musteritable.Columns.Add ("yas",System.Type.GetType("System.Int32"))
'Tabloyu DataSete Ekle --------
ds.Tables.Add(musteritable)
'Satir Olusturup, Veri Gir-------
Dim sira As DataRow = musteritable.NewRow
sira("ad")="Zafer"
sira("soyad")="Demirkol"
sira("meslek")="Web Programcýsý"
sira("yas")="33"
musteritable.rows.Add(sira)
Dim sira1 As DataRow = musteritable.NewRow
sira("ad")="Serdar"
sira("soyad")="Benli"
sira("meslek")="Pazarlamacý"
sira("yas")="32"
musteritable.rows.Add(sira1)
Dim sira2 As DataRow = musteritable.NewRow
sira("ad")="Denizhan"
sira("soyad")="Akkaya"
sira("meslek")="Muhasebeci"
sira("yas")="26"
musteritable.rows.Add(sira2)
Dim sira3 As DataRow = musteritable.NewRow
sira("ad")="Levent"
sira("soyad")="Demirkol"
sira("meslek")="Aþcý"
sira("yas")="28"
musteritable.rows.Add(sira3)
'----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")
Dim satir_3 As Datarow
satir_3 = ds.Tables("musteriler").rows(2)
Response.Write(satir_3(0) & "<br>")
Dim siralar As DataRowCollection
siralar=ds.Tables("musteriler").rows
Response.Write(siralar(2)("ad") & "<br>")
Dim tablo_degiskenim As DataTable
tablo_degiskenim = ds.Tables("musteriler")
Response.Write(tablo_degiskenim.rows(2)(0) & "<br>")
End sub
</script
AND THE ERROR PAGE;
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 43: musteritable.rows.Add(sira3)
Line 44: '----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Line 45: Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")
Line 46:
Line 47: Dim satir_3 As Datarow
Source File: D:\ASP.NET\dataset_olustur2.aspx Line: 45
I COULDN'T SEE THE MISTAKE, CAN YOU HELP ME?
THANX ...When you created the table, you called it "müsteriler".
Then, when you referenced it on Line 45, you called it "musteriler" ... with no accent ...
Oh yeah ! :)
...
I am bad at my native-lang "Turkish" just like I am bad at English ...
ü is a fuckin' letter in our keyboards ...
...
Thanx NewKid2 ...
If you find this happening too often, considerchanging the language of your computer.
Thursday, March 22, 2012
Object reference not set to an instance of an object.
I am getting an 'System.NullReferenceException: Object reference not set to
an instance of an object.' error on the following line;
Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
occupation) " & _ '<== This line gives the error.
"VALUES('" & txtTitle.Text & "', '" &
txtForenames.Text & "', '" & txtSurname.Text & "', '" &
txtOccupation.SelectedItem.Text & "')"
What am I doing wrong?
Thanks
Regardshi john,
The line of code that you gave below might not throw this exception. If you
can paste a few lines below and after it would be much clear.
the best
srini
"John" wrote:
> Hi
> I am getting an 'System.NullReferenceException: Object reference not set to
> an instance of an object.' error on the following line;
> Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
> occupation) " & _ '<== This line gives the error.
> "VALUES('" & txtTitle.Text & "', '" &
> txtForenames.Text & "', '" & txtSurname.Text & "', '" &
> txtOccupation.SelectedItem.Text & "')"
> What am I doing wrong?
> Thanks
> Regards
>
Friday, March 16, 2012
Object reference not set to an instance of an object.
When using the this code
<%@dotnet.itags.org. Page Language="VB" %>
<%@dotnet.itags.org. import Namespace="System.Data" %>
<%@dotnet.itags.org. import Namespace="System.Data.SqlClient" %>
<script runat="server"
Sub Button2_Click(sender As Object, e As EventArgs)Dim conn As new sqlconnection ("server='sql2.redstation.co.uk'; user id='2128779.150'; password='CLT42876'; database='2128779.150'")
dim dbcommanda as new sqlcommand("SELECT * from tblReg", conn)
dim myDataSet as New Dataset()
dim daprods as new sqldataadapter(dbcommanda)
daprods.fill(myDataSet,"Tony")
if myDataSet.Tables("Details").rows.count = 0 then
response.write("Empty")
end ifconn.close()
I get this error
Server Error in '/' Application.
------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Can Anyone tell me what i am doing wrong?
rgs
TonyPlease specify what line the error occurs on. That will make it lots easier to diagnose.
At first glance, I think the problem might be that you are trying to reference a datatable in the dataset that doesn't exist - Details. When you filled the dataset...
daprods.fill(myDataSet,"Tony")
.. a datatable was created named Tony. The data would then have been loaded into that table. Next, you try to get info about a datatable named Details...
if myDataSet.Tables("Details").rows.count = 0 then
.. which doesn't exist. Two things you can do in order to test this theory:
1. don't provide any datatable name during fill():
daprods.fill(myDataSet).
Then reference the datatable by its ordinal value:
if myDataSet.Tables(0).rows.count = 0 then
2. Try change Tony to Details: daprods.fill(myDataSet,"Details")
Hopefully this helps or at least points you in the right direction.
Let me know if this works :-]
The problem appears to be that in your Fill call, you're telling it to fill a table called Tony. In your check for an empty return, you're checking against a table called Details. There is no Details table in your dataset, so you're trying to reference an object that doesn't exist, which is why you're getting that error.
JBR thanks
ProgUser....thanks for the tips...worked a treat
Object reference not set to an instance of an object.
I am getting an 'System.NullReferenceException: Object reference not set to
an instance of an object.' error on the following line;
Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
occupation) " & _ '<== This line gives the error.
"VALUES('" & txtTitle.Text & "', '" &
txtForenames.Text & "', '" & txtSurname.Text & "', '" &
txtOccupation.SelectedItem.Text & "')"
What am I doing wrong?
Thanks
Regardshi john,
The line of code that you gave below might not throw this exception. If you
can paste a few lines below and after it would be much clear.
the best
srini
"John" wrote:
> Hi
> I am getting an 'System.NullReferenceException: Object reference not set t
o
> an instance of an object.' error on the following line;
> Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname
,
> occupation) " & _ '<== This line gives the error.
> "VALUES('" & txtTitle.Text & "', '" &
> txtForenames.Text & "', '" & txtSurname.Text & "', '" &
> txtOccupation.SelectedItem.Text & "')"
> What am I doing wrong?
> Thanks
> Regards
>
>