Saturday, March 24, 2012
System.NullReferenceException
system.nullreferenceexception every time i click button to add data to
datatable
here below is my code :
Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable
Private Sub Page_Load(s As Object, e As EventArgs)
if Not IsPostBack Then
make_d_tables()
End if
End Sub
sub make_d_tables()
wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))
zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))
End sub
Sub Button2_Click(sender As Object, e As EventArgs)
row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(row)
row = wykszt.NewRow
Mxdatagrid1.datasource=wykszt
Mxdatagrid1.databind
End Sub
Thanks
PatYou have to recreate your tables every time. So take out the if statement
from page_load.
"ven" <venome_@.poczta.onet.pl> wrote in message
news:cif6p8$sf3$1@.news.onet.pl...
> How must i declare my variables to have this code work. I get
> system.nullreferenceexception every time i click button to add data to
> datatable
> here below is my code :
> Dim wykszt as System.Data.DataTable
> Dim zatru as System.Data.DataTable
> Private Sub Page_Load(s As Object, e As EventArgs)
> if Not IsPostBack Then
> make_d_tables()
> End if
> End Sub
> sub make_d_tables()
> wykszt = New System.Data.DataTable("wykszt")
> wykszt.Columns.Add("od_data", GetType(date))
> wykszt.Columns.Add("do_data", GetType(date))
> wykszt.Columns.Add("nazwa_szkoly", GetType(String))
> wykszt.Columns.Add("nazwa_tytulu", GetType(String))
> zatru = New System.Data.DataTable("zatr")
> zatru.Columns.Add("od_data", GetType(date))
> zatru.Columns.Add("do_data", GetType(date))
> zatru.Columns.Add("nazwa_prac", GetType(String))
> zatru.Columns.Add("nazwa_stan", GetType(String))
> End sub
> Sub Button2_Click(sender As Object, e As EventArgs)
> row = wykszt.NewRow
> row("od_data")=TextBox27.Text + "-01"
> row("do_data")=TextBox28.Text + "-01"
> row("nazwa_szkoly")=TextBox29.Text
> row("nazwa_tytulu")=TextBox30.Text
> wykszt.Rows.Add(row)
> row = wykszt.NewRow
> Mxdatagrid1.datasource=wykszt
> Mxdatagrid1.databind
> End Sub
>
> Thanks
> Pat
>
Which line is throwing the exception?
Does the error only happen on postback?
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"ven" <venome_@.poczta.onet.pl> wrote in message
news:cif6p8$sf3$1@.news.onet.pl...
> How must i declare my variables to have this code work. I get
> system.nullreferenceexception every time i click button to add data to
> datatable
> here below is my code :
> Dim wykszt as System.Data.DataTable
> Dim zatru as System.Data.DataTable
> Private Sub Page_Load(s As Object, e As EventArgs)
> if Not IsPostBack Then
> make_d_tables()
> End if
> End Sub
> sub make_d_tables()
> wykszt = New System.Data.DataTable("wykszt")
> wykszt.Columns.Add("od_data", GetType(date))
> wykszt.Columns.Add("do_data", GetType(date))
> wykszt.Columns.Add("nazwa_szkoly", GetType(String))
> wykszt.Columns.Add("nazwa_tytulu", GetType(String))
> zatru = New System.Data.DataTable("zatr")
> zatru.Columns.Add("od_data", GetType(date))
> zatru.Columns.Add("do_data", GetType(date))
> zatru.Columns.Add("nazwa_prac", GetType(String))
> zatru.Columns.Add("nazwa_stan", GetType(String))
> End sub
> Sub Button2_Click(sender As Object, e As EventArgs)
> row = wykszt.NewRow
> row("od_data")=TextBox27.Text + "-01"
> row("do_data")=TextBox28.Text + "-01"
> row("nazwa_szkoly")=TextBox29.Text
> row("nazwa_tytulu")=TextBox30.Text
> wykszt.Rows.Add(row)
> row = wykszt.NewRow
> Mxdatagrid1.datasource=wykszt
> Mxdatagrid1.databind
> End Sub
>
> Thanks
> Pat
>
Uytkownik "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> napisa w wiadomoci
news:OkGuVzNnEHA.1304@.TK2MSFTNGP09.phx.gbl...
> Which line is throwing the exception?
> Does the error only happen on postback?
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> "ven" <venome_@.poczta.onet.pl> wrote in message
> news:cif6p8$sf3$1@.news.onet.pl...
>
sorry i left behind that i have Dim row as System.Data.DataRow in my
declarations too
but exception is only when i post back and
on this line :
row = wykszt.NewRow
and a full stack :
Szczegy wyjtku: System.NullReferenceException: Odwoanie do obiektu nie
zostao ustawione na wystpienie obiektu.
Bd rda:
Wiersz 35: Sub Button2_Click(sender As Object, e As EventArgs)
Wiersz 36:
Wiersz 37: row = wykszt.NewRow
Wiersz 38: row("od_data")=TextBox27.Text + "-01"
Wiersz 39: row("do_data")=TextBox28.Text + "-01"
Plik rdowy: c:\inetpub\wwwroot\axell\forma.aspx Wiersz: 37
lad stosu:
[NullReferenceException: Odwoanie do obiektu nie zostao ustawione na
wystpienie obiektu.]
ASP.forma_aspx.Button2_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\axell\forma.aspx:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument) +58
System.Web.UI.Page. RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
sorry for polish language
Thanks
Pat
In that case I agree with Marina.
You need to recreate your table upon each postback.
Right now you aren't doing that, so you can't add a row to a table that
doesn't exist, thus your error.
Remove the If statement in Page_Load.
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
> sorry i left behind that i have Dim row as System.Data.DataRow in my
> declarations too
> but exception is only when i post back and
> on this line :
> row = wykszt.NewRow
> and a full stack :
> Szczegy wyjtku: System.NullReferenceException: Odwoanie do obiektu nie
> zostao ustawione na wystpienie obiektu.
> Bd rda:
> Wiersz 35: Sub Button2_Click(sender As Object, e As EventArgs)
> Wiersz 36:
> Wiersz 37: row = wykszt.NewRow
> Wiersz 38: row("od_data")=TextBox27.Text + "-01"
> Wiersz 39: row("do_data")=TextBox28.Text + "-01"
>
> Plik rdowy: c:\inetpub\wwwroot\axell\forma.aspx Wiersz: 37
> lad stosu:
> [NullReferenceException: Odwoanie do obiektu nie zostao ustawione na
> wystpienie obiektu.]
> ASP.forma_aspx.Button2_Click(Object sender, EventArgs e) in
> c:\inetpub\wwwroot\axell\forma.aspx:37
> System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
> System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.Raise
Po
> stBackEvent(String eventArgument) +58
> System.Web.UI.Page. RaisePostBackEvent(IPostBackEventHandler
> sourceControl, String eventArgument) +18
> System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
> System.Web.UI.Page.ProcessRequestMain() +1277
> sorry for polish language
> Thanks
> Pat
>
System.NullReferenceException
system.nullreferenceexception every time i click button to add data to
datatable
here below is my code :
Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable
Private Sub Page_Load(s As Object, e As EventArgs)
if Not IsPostBack Then
make_d_tables()
End if
End Sub
sub make_d_tables()
wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))
zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))
End sub
Sub Button2_Click(sender As Object, e As EventArgs)
row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(row)
row = wykszt.NewRow
Mxdatagrid1.datasource=wykszt
Mxdatagrid1.databind
End Sub
Thanks
PatYou have to recreate your tables every time. So take out the if statement
from page_load.
"ven" <venome_@.poczta.onet.pl> wrote in message
news:cif6p8$sf3$1@.news.onet.pl...
> How must i declare my variables to have this code work. I get
> system.nullreferenceexception every time i click button to add data to
> datatable
> here below is my code :
> Dim wykszt as System.Data.DataTable
> Dim zatru as System.Data.DataTable
> Private Sub Page_Load(s As Object, e As EventArgs)
> if Not IsPostBack Then
> make_d_tables()
> End if
> End Sub
> sub make_d_tables()
> wykszt = New System.Data.DataTable("wykszt")
> wykszt.Columns.Add("od_data", GetType(date))
> wykszt.Columns.Add("do_data", GetType(date))
> wykszt.Columns.Add("nazwa_szkoly", GetType(String))
> wykszt.Columns.Add("nazwa_tytulu", GetType(String))
> zatru = New System.Data.DataTable("zatr")
> zatru.Columns.Add("od_data", GetType(date))
> zatru.Columns.Add("do_data", GetType(date))
> zatru.Columns.Add("nazwa_prac", GetType(String))
> zatru.Columns.Add("nazwa_stan", GetType(String))
> End sub
> Sub Button2_Click(sender As Object, e As EventArgs)
> row = wykszt.NewRow
> row("od_data")=TextBox27.Text + "-01"
> row("do_data")=TextBox28.Text + "-01"
> row("nazwa_szkoly")=TextBox29.Text
> row("nazwa_tytulu")=TextBox30.Text
> wykszt.Rows.Add(row)
> row = wykszt.NewRow
> Mxdatagrid1.datasource=wykszt
> Mxdatagrid1.databind
> End Sub
>
> Thanks
> Pat
Which line is throwing the exception?
Does the error only happen on postback?
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"ven" <venome_@.poczta.onet.pl> wrote in message
news:cif6p8$sf3$1@.news.onet.pl...
> How must i declare my variables to have this code work. I get
> system.nullreferenceexception every time i click button to add data to
> datatable
> here below is my code :
> Dim wykszt as System.Data.DataTable
> Dim zatru as System.Data.DataTable
> Private Sub Page_Load(s As Object, e As EventArgs)
> if Not IsPostBack Then
> make_d_tables()
> End if
> End Sub
> sub make_d_tables()
> wykszt = New System.Data.DataTable("wykszt")
> wykszt.Columns.Add("od_data", GetType(date))
> wykszt.Columns.Add("do_data", GetType(date))
> wykszt.Columns.Add("nazwa_szkoly", GetType(String))
> wykszt.Columns.Add("nazwa_tytulu", GetType(String))
> zatru = New System.Data.DataTable("zatr")
> zatru.Columns.Add("od_data", GetType(date))
> zatru.Columns.Add("do_data", GetType(date))
> zatru.Columns.Add("nazwa_prac", GetType(String))
> zatru.Columns.Add("nazwa_stan", GetType(String))
> End sub
> Sub Button2_Click(sender As Object, e As EventArgs)
> row = wykszt.NewRow
> row("od_data")=TextBox27.Text + "-01"
> row("do_data")=TextBox28.Text + "-01"
> row("nazwa_szkoly")=TextBox29.Text
> row("nazwa_tytulu")=TextBox30.Text
> wykszt.Rows.Add(row)
> row = wykszt.NewRow
> Mxdatagrid1.datasource=wykszt
> Mxdatagrid1.databind
> End Sub
>
> Thanks
> Pat
Uytkownik "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> napisa w wiadomoci
news:OkGuVzNnEHA.1304@.TK2MSFTNGP09.phx.gbl...
> Which line is throwing the exception?
> Does the error only happen on postback?
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> "ven" <venome_@.poczta.onet.pl> wrote in message
> news:cif6p8$sf3$1@.news.onet.pl...
> > How must i declare my variables to have this code work. I get
> > system.nullreferenceexception every time i click button to add data to
> > datatable
> > here below is my code :
> > Dim wykszt as System.Data.DataTable
> > Dim zatru as System.Data.DataTable
> > Private Sub Page_Load(s As Object, e As EventArgs)
> > if Not IsPostBack Then
> > make_d_tables()
> > End if
> > End Sub
> > sub make_d_tables()
> > wykszt = New System.Data.DataTable("wykszt")
> > wykszt.Columns.Add("od_data", GetType(date))
> > wykszt.Columns.Add("do_data", GetType(date))
> > wykszt.Columns.Add("nazwa_szkoly", GetType(String))
> > wykszt.Columns.Add("nazwa_tytulu", GetType(String))
> > zatru = New System.Data.DataTable("zatr")
> > zatru.Columns.Add("od_data", GetType(date))
> > zatru.Columns.Add("do_data", GetType(date))
> > zatru.Columns.Add("nazwa_prac", GetType(String))
> > zatru.Columns.Add("nazwa_stan", GetType(String))
> > End sub
> > Sub Button2_Click(sender As Object, e As EventArgs)
> > row = wykszt.NewRow
> > row("od_data")=TextBox27.Text + "-01"
> > row("do_data")=TextBox28.Text + "-01"
> > row("nazwa_szkoly")=TextBox29.Text
> > row("nazwa_tytulu")=TextBox30.Text
> > wykszt.Rows.Add(row)
> > row = wykszt.NewRow
> > Mxdatagrid1.datasource=wykszt
> > Mxdatagrid1.databind
> > End Sub
> > Thanks
> > Pat
sorry i left behind that i have Dim row as System.Data.DataRow in my
declarations too
but exception is only when i post back and
on this line :
row = wykszt.NewRow
and a full stack :
Szczegy wyjtku: System.NullReferenceException: Odwoanie do obiektu nie
zostao ustawione na wystpienie obiektu.
Bd rda:
Wiersz 35: Sub Button2_Click(sender As Object, e As EventArgs)
Wiersz 36:
Wiersz 37: row = wykszt.NewRow
Wiersz 38: row("od_data")=TextBox27.Text + "-01"
Wiersz 39: row("do_data")=TextBox28.Text + "-01"
Plik rdowy: c:\inetpub\wwwroot\axell\forma.aspx Wiersz: 37
lad stosu:
[NullReferenceException: Odwoanie do obiektu nie zostao ustawione na
wystpienie obiektu.]
ASP.forma_aspx.Button2_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\axell\forma.aspx:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
stBackEvent(String eventArgument) +58
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
sorry for polish language
Thanks
Pat
In that case I agree with Marina.
You need to recreate your table upon each postback.
Right now you aren't doing that, so you can't add a row to a table that
doesn't exist, thus your error.
Remove the If statement in Page_Load.
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
> sorry i left behind that i have Dim row as System.Data.DataRow in my
> declarations too
> but exception is only when i post back and
> on this line :
> row = wykszt.NewRow
> and a full stack :
> Szczegy wyjtku: System.NullReferenceException: Odwoanie do obiektu nie
> zostao ustawione na wystpienie obiektu.
> Bd rda:
> Wiersz 35: Sub Button2_Click(sender As Object, e As EventArgs)
> Wiersz 36:
> Wiersz 37: row = wykszt.NewRow
> Wiersz 38: row("od_data")=TextBox27.Text + "-01"
> Wiersz 39: row("do_data")=TextBox28.Text + "-01"
>
> Plik rdowy: c:\inetpub\wwwroot\axell\forma.aspx Wiersz: 37
> lad stosu:
> [NullReferenceException: Odwoanie do obiektu nie zostao ustawione na
> wystpienie obiektu.]
> ASP.forma_aspx.Button2_Click(Object sender, EventArgs e) in
> c:\inetpub\wwwroot\axell\forma.aspx:37
> System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
> System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
> stBackEvent(String eventArgument) +58
> System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
> sourceControl, String eventArgument) +18
> System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
> System.Web.UI.Page.ProcessRequestMain() +1277
> sorry for polish language
> Thanks
> Pat
Thursday, March 22, 2012
system.nullreferenceexception problem
can anybody solve or tell more about this problem
System.NullReferenceException: Object reference not set to an instance of an object
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.ApplicationBlocks.UIProcess.WebFormViewManager.GetViewNameForCurrentRequest(IView currentView) +99
Microsoft.ApplicationBlocks.UIProcess.Navigator.GetControllerForView(IView view) +89
Microsoft.ApplicationBlocks.UIProcess.WebFormView.WebFormView_Load(Object sender, EventArgs e) +312
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +733
Version Information: Microsoft .NET Framework Version:1.1.4322.2300; ASP.NET Version:1.1.4322.2300
thanks
this is not enough info to tell you exactly what you are doing wrong, but - you are calling on a variable that you did not assign a value to. It could be something as simple as:
int a; // giving it no value
int b = a; // as you can see, you didnt *** value into a, so you are calling on a null value
this is not enough info to tell you exactly what you are doing wrong, but - you are calling on a variable that you did not assign a value to. It could be something as simple as:
int a; // giving it no value
int b = a; // as you can see, you didnt put a value into a, so you are calling on a null value
Please provide the code so we can look into the problem!
Thanks for replies.
actually i m totally new to asp.net. i have to modify an already existing project. so its more confusing bcoz i dont know very much about the actual code. the strange thing i notice that website run properly for some time at starting. and after some time it will show nothing except the system.nullreference exception on each page. does this information help to u understand the problem??
is it related to something like cache expired? i dont know abt this problem and that's why i m searching net for a solution.
thanks
System.NullReferenceException: Object reference not set to an instance of an object.
under Page_Load at Default.aspx:
loginBox.LoginAuthenticated += new LoginAuthenticatedEventHandler(Succeeded)under my LoginBox.ascx.cs, I setup the object like the followings:
public delegate void LoginAuthenticatedEventHandler(object sender, LoginAuthenticatedEventArgs e);
public event LoginAuthenticatedEventHandler LoginAuthenticated;
public class LoginAuthenticatedEventArgs : EventArgs
{
public int UsrID;
}//end class LoginAuthenticatedEventArgs
under my Page_Load of my LoginBox.ascx.cs, I load my object like this:
if (LoginAuthenticated != null)
{
LoginAuthenticatedEventArgs eventInfo = new LoginAuthenticatedEventArgs();
eventInfo.UsrID = Int32.Parse(txtUser.Text);
LoginAuthenticated(Page, eventInfo);
}//end if (LoginAuthenticated != null)
I thought I had the object reference to the correct place, what else am I missing?
TIA
..resoloved: http://forums.asp.net/1089126/ShowPost.aspx
System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
line: finalVal = finalVal + Convert.ToInt64(strAI.Text)
Code--------
' Grab the textbox values of the interest cost table, loop through them adding up the values and displaying the totals
PrivateSub btnInterestCostTotal_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnInterestCostTotal.Click
'AddField(DS_Term.Text)
Dim C, NAsInteger
'Find the txtAnnualInterestTotal textbox
Dim AITAs TextBox =CType(myTable.FindControl("txtAnnualInterestTotal"), TextBox)
Dim finalValAs Int64 = 0
'Loop through the dynamic textboxes
For C = 0ToCInt(DS_Term.Text) - 1
N = N + 1
Dim strAIAs TextBox =CType(myTable.FindControl("txtAnnualInterest" & N), TextBox)
'Add all the dynamic textboxes up
finalVal = finalVal + Convert.ToInt64(strAI.Text)
Next
'Display the total value in the txtAnnualInterestTotal textbox
AIT.Text = Convert.ToString(finalVal)
EndSub
code------------
I believe I am. Here is the code I am adding to the Page Init. Am I corrected adding them?
Sub createDynamicFields()
' Create the table and the controls based on the Term(# of years)
Dim IAsInteger
Dim intCounterAs Int64
If DS_Term.Text <> ""Then
For I = 0ToCInt(DS_Term.Text) - 1
Dim lblFiscalYearAs LiteralControl =New LiteralControl
Dim txtTextBoxAs TextBox =New TextBox
Dim txtTextBox2As TextBox =New TextBox
Dim txtTextBox3As TextBox =New TextBox
Dim tRow2As TableRow =New TableRow
Dim tCell1As TableCell =New TableCell
Dim tCell2As TableCell =New TableCell
Dim tCell3As TableCell =New TableCell
Dim tCell4As TableCell =New TableCell
' Grab Fiscal Year from tbBeginngingFiscalYear and Incurment the date by the DS_BondTerm field
lblFiscalYear.ID = "lblFiscalYear" & I + 1
lblFiscalYear.Text = Convert.ToString(Convert.ToInt64(DS_BeginningFiscalYear.Text) + I)
'**********************************
'Set the column width and
txtTextBox.Columns = 15
txtTextBox2.Columns = 15
txtTextBox3.Columns = 15
'Assign the textboxes IDs
txtTextBox.ID = "txtInterest" & I + 1
txtTextBox2.ID = "txtOsPrincipal" & I + 1
txtTextBox3.ID = "txtAnnualInterest" & I + 1
'Assign pre-defined values
'txtTextBox.Text = CStr(I + 1)
'txtTextBox2.Text = CStr(I + 1)
'txtTextBox3.Text = CStr(I + 1)
'Assign the cells to a row
tRow2.Cells.Add(tCell1)
tRow2.Cells.Add(tCell2)
tRow2.Cells.Add(tCell3)
tRow2.Cells.Add(tCell4)
'Assign the row to the table
myTable.Rows.Add(tRow2)
'Create the new controls
tCell1.Controls.Add(lblFiscalYear)
tCell2.Controls.Add(txtTextBox)
tCell3.Controls.Add(txtTextBox2)
tCell4.Controls.Add(txtTextBox3)
'Add the table to the placeholder
plhBondTerms.Controls.Add(myTable)
myTable.CellPadding = 0
myTable.CellSpacing = 0
'intCounter = intCounter + 1
'ViewState("Count") = intCounter
Next I
' ************************************
EndIf
EndSub
you should call this method on every postback
System.NullReferenceException: Object reference not set to an instance of an object.
This is the error message I get:
System.NullReferenceException: Object reference not set to an instance of an object.
at MyStore.CustomerFunctions.getCustomerOrders(String CustomerID)
at MyStore.Customer_Orders.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
And this is my code:
PublicSharedFunction getCustomerOrders(ByVal CustomerIDAsString)As OrderCollection
Dim strSQLAsString
Dim m_strConnectionStringAsString
Dim MyReaderAs SqlDataReader
Dim mySqlParaAs SqlParameter
Dim MyOrdersAsNew OrderCollection
m_strConnectionString = ConfigurationSettings.AppSettings("ConnectionString_checkout")
strSQL = "sp_WWWgetCustomerOrders"
mySqlPara =New SqlParameter("@dotnet.itags.org.CustomerID", CustomerID)
Try
MyReader = SqlHelper.ExecuteReader(m_strConnectionString, CommandType.StoredProcedure, strSQL, mySqlPara)
MyOrders.Fill(MyReader)
Catch exAs Exception
' Rethrow Exception for the moment
Throw ex
Finally
MyReader.Close()
MyReader =Nothing
EndTry
Return MyOrders
EndFunction
Code that calls the method:
DimaCustomerAs Customer
Dim MyOrdersAs OrderCollection
aCustomer =CType(HttpContext.Current.User, CustomPrincipal).Customer
MyOrders = CustomerFunctions.getCustomerOrders(aCustomer.CustomerID)
If MyOrders.Count > 0Then
Me.rptOpenRecentOrders.DataSource = MyOrders
Me.rptOpenRecentOrders.DataBind()
pnlNoOrdersToDisplay.Visible =False
Else
pnlNoOrdersToDisplay.Visible =True
EndIf
I only get the error occasionally at busy times on the internet, the Stored Proc is valid the connection string is valid because it works some times. The problem is not incorrect data being passed because if no data or incorrect data is past a different exception will be displayed because I have tested. I have no idea why this is happening, I have a feeling its to do with SQL Server because it only happens when it is at its busiest.
If the Stored Proc returns an empty reader there is no error, I have alreadyinitialized My returning object (the Orders Collection) so it shouldn't matter.
I know that the customer id is valid because I have an email sent to me if there is an error in the application via the globalApplication_Errormethod, it gets the customer id from the currently logged in person
Dim aCustomerAs Customer =CType(HttpContext.Current.User, CustomPrincipal).Customer
strCustID = aCustomer.CustomerID
This is displayed correctly when emailed. As I say this only happens at busy times and I am pulling my hair out to identify whats happening, can any one help?
Cheers
Scott
Regarding this...
elbandit wrote:
...As I say this only happens at busy times and I am pulling my hair out to identify whats happening, can any one help?
...I am wondering-- exactly what line is causing the error?
(The line can be discovered by doing a step-through of the code.)
I am going to guess that it is this line...
Do While MyReader.Read = True
...that causes the error.
If so, then you may be able to check for Nothing before the operation, like this...
If (MyReader Is Nothing) Then
'Reader cannot be used, probably due to a database connection issue.
'Maybe retry 1x automatically and if no connection then show message to user and ask the user to retry.
Else
Do While MyReader.Read = True
'Continue.
End If
...which may or may not work based on your use case.
HTH.
Thank you.
--Mark Kamoski
Sorry Mark I posted the wrong function, I have now editied. Please can you have another look,
Thanks
Scott
i can not see in your code
Dim con as New Sqlconenction(connection string her)
is it some where else or that what you miss!!??
elbandit--
Regarding this...
elbandit wrote:
Sorry Mark I posted the wrong function, I have now editied. Please can you have another look...
...I still think that the issue may be that the Reader is not getting instantiated properly when then database is too busy. As I mentioned above, you may need to check for null (Nothing) before using it.
If the database is busy, you may need to increase the timeout (a property on the connection string, seehttp://www.ConnectionStrings.com for details).
Alternately, if the database is really busy maybe your licensing has reached the maximum number of of connections and that's why the Reader is not instantiated properly.
Another idea would be to use a DataAdapter and a DataSet. Yours is a simple case and these objects might be a bit more stable given that the Reader stays open until it is closed and you really do not need an open connection to the database in this case.
The code looks something like this...
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(COMMAND_TEXT, myConnection);
System.Data.OleDb.OleDbDataAdapter myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
...or you can seehttp://www.WebLogicArts.com/DemoReadingData01.aspx for a full working sample with all the code.
HTH.
Thank you.
--Mark Kamoski
Thanks Mark, I think you are probably right, as it only errors in busy periods I have changed the code to:
MyReader = SqlHelper.ExecuteReader(m_strConnectionString, CommandType.StoredProcedure, strSQL, mySqlPara)
If MyReaderIsNothingThen
Email.sendEmail()
EndIf
MyOrders.Fill(MyReader)
This way it will still error but I will get an email if the problem is that the reader is null, then I will be able to test with populating a dataset then populating my custom collection from that. The reason I didn't do it like that to begin with is that I thought a reader populated a dataset, but I guess if that is true the reader will populate a dataset faster than could populate my custom collection and therefore no error.
I will check out the time out property as well and let you know how I got on, and post the code that works. Cheers.
Thanks for the replies
Scott
P.S.
Fadil, I use MS Data Access Application Block to handle all connections to the database and opening the database and closing it is all done within in the most effiecient manner take a look athttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/daab.asp for more info.
System.NullReferenceException: Object reference not set to an instance of an object.
This is the error i am getting, it says to review the stack trace. Here is the stack trace with code from my vb file below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Manager.bindData(String sortExpression) +52
Manager.Page_Load(Object sender, EventArgs e) +13
System.Web.UI.Control.OnLoad(EventArgs e) +55
System.Web.UI.Control.LoadRecursive() +27
System.Web.UI.Page.ProcessRequestMain() +750
Private Sub Page_Load(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
If not ispostback then
bindData("BugFind.BugID")
End If
end sub
Private Sub bindData(ByVal sortExpression as String)
Dim uEmail as string
uEmail = Session("Email").ToString()
dim conn as OleDbConnection
dim adapter as OleDbDataAdapter
dim mydataset as DataSet
dim connstring, query as string
Try
connstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="& server.mappath("Database.mdb")
conn = new OleDbConnection(connstring)
conn.Open()
query = "Select [Products].[ProductName], [BugFind].[FeatureName], [BugFind].[FinderEmail], [BugFind].[BuildNumber], [BugFind].[MajorReleaseNumber], [BugFind].[MinorReleaseNumber], [BugFind].[FindDate], [BugFind].[BugID], [BugFind].[FindTime] FROM [BugFind], [Products] where [BugFind].[ProductID] = [Products].[Product ID] and [Products].[ManagerEmail] = ('" & uEmail & "') ORDER BY " & sortExpression
adapter = new OleDbDataAdapter(query, conn)
mydataset = new dataset
adapter.Fill(mydataset)
BugsAssigned.DataKeyField = "BugID"
BugsAssigned.DataSource = mydataset
BugsAssigned.DataBind()
Finally
If (not isnothing(conn)) then
conn.close()
End if
End Try
End Sub
I really need help, because this is happening on another page of mine also and its head wrecking. I can't fix it.couple things to check...
::uEmail = Session("Email").ToString()
if Email does not exist in your session, your uEmail will be null/nothing
::BugsAssigned.DataKeyField = "BugID"
What is BugsAssigned, and does it exist (i.e. was an instance created...)?
Hey cheers for the reply. I have changed the uEmail string to a hard coded email address fopr the time being. You brought to my attention:
BugsAssigned.DataKeyField = "BugID"
What is BugsAssigned, and does it exist (i.e. was an instance created...)?
BugAssigned was an incorrect id for my DataGrid. It should be BugTable, but when i renamed it to BugTable it gave me this error:
Name 'BugTable' is not declared.
mydataset = new dataset
Line 46: adapter.Fill(mydataset)
Line 47: BugTable.DataKeyField = "BugID"
Line 48: BugTable.DataSource = mydataset
Line 49: BugTable.DataBind()
Hey cheers for the reply. I have changed the uEmail string to a hard coded email address fopr the time being. You brought to my attention:
BugsAssigned.DataKeyField = "BugID"
What is BugsAssigned, and does it exist (i.e. was an instance created...)?
BugAssigned was an incorrect id for my DataGrid. It should be BugTable, but when i renamed it to BugTable it gave me this error:
Name 'BugTable' is not declared.
mydataset = new dataset
Line 46: adapter.Fill(mydataset)
Line 47: BugTable.DataKeyField = "BugID"
Line 48: BugTable.DataSource = mydataset
Line 49: BugTable.DataBind()
Well, where did you declare BugTable?
its ok, i fixed the problem. BugTable was spelled wrongly for the id of my datagrid.cheers for the help guys
System.NullReferenceException: Object reference not set to an instance of an object.
I have a component that returns a value from a Sql Count statement... unfortunately I get the following error: System.NullReferenceException: Object reference not set to an instance of an object.
My component code:
Function GetCount(varAccountNumber as String) as String
conSqlConnect = New SqlConnection( "..." )
conSqlConnect.Open()
cmdSelect = New SqlCommand( "SELECT Count([account number]) AS Total From some_table Where [account number]=@dotnet.itags.org.accountnumber", conSqlConnect )
cmdSelect.Parameters.Add( "@dotnet.itags.org.accountnumber", varAccountNumber )
Return (cmdselect.executescalar())
conSqlConnect.Close()
End Function
on my code-behind page. I have
If myComponent.GetCount(user.identity.name) = 0 Then
'do something
End If
any suggestions?Which line caused this error?
>>Which line caused this error?
If myComponent.GetCount(user.identity.name) = 0 Then
What is "myComponent" ? It seems that it is not set correctly. Make sure that you initialize it the way it needs to be.
Yes I did initialize mycomponent like so:
Dim MyComponent as MyComponentClass
myComponent = New MyComponentClass()
any suggestions?
Either the component or the user.identity.name is causing the error. Try to debug the GetCount function of your component and also check if user.identity.name contains any value. At last I would say that debugging is the only way. Try quickwatch in Visual Studio.
Hope that it helped at least a little bit.
System.NullReferenceException: Object variable or With block variable not set
System.NullReferenceException: Object variable or With block variable not set
on this line.
TextBox1.Text = Trim(Row.Item("eng_lbl"))
I am trying to display the values of a record from the table in 4 text box fields.
I am new to asp.net. Please kindly help.
You're not assign row to anything..
Dim mydataset As New DataSet
Dim myadapter As New SqlDataAdapter
Dim strId As String = Request.QueryString("lbl_Id")
Dim RowDim myCommand As New SqlClient.SqlCommand
Dim sql As String = "select * from tbl_labels where id =" & CInt(strId)
If Not Page.IsPostBack Then
MyCommand = New SqlCommand(sql, MyConnection)myadapter = New SqlDataAdapter(myCommand)
mydataset.Clear()
myadapter.Fill(mydataset)If mydataset.Tables(0).Rows.Count > 0 Then
TextBox1.Text = Trim(Row.Item("eng_lbl"))
TextBox2.Text = Trim(Row.Item("esp_lbl"))
TextBox3.Text = Trim(Row.Item("fr_lbl"))
TextBox4.Text = Trim(Row.Item("ger_lbl"))
End If
End If
I.E.
Dim Row As DataRowIf mydataset.Tables(0).Rows.Count > 0 Then
Row = mydataset.Tables(0).Rows(0)
TextBox1.Text = Trim(Row.Item("eng_lbl"))
TextBox2.Text = Trim(Row.Item("esp_lbl"))
TextBox3.Text = Trim(Row.Item("fr_lbl"))
TextBox4.Text = Trim(Row.Item("ger_lbl"))
End If