Showing posts with label isobject. Show all posts
Showing posts with label isobject. Show all posts

Thursday, March 29, 2012

Object reference

The error is:
Object reference not set to an instance of an object. (See commented line below---)
I tried using the new keyword and that does not solve the problem.
Do you see the problem?

Private Sub LoadData(ByVal SortKey As String)

Dim strPath As String = Server.MapPath(Request.ApplicationPath)
Dim ds As New DataSet()
ds.ReadXmlSchema(strPath & "\XML\A.xsd")
ds.ReadXml(strPath & "\XML\A.xml")

'------This line gives the error------------
Dim view As DataView = ds.Tables("Orders").DefaultView
'------------------
If Not (SortKey Is Nothing) And SortKey <> [String].Empty Then
view.Sort = SortKey
End If
dgUsers.DataSource = view
dgUsers.DataBind()
End SubOne problem i see is that you never name a table "Orders" but try to reference it anyway. If you only have one table try referencing ds.Tables(0).DefaultView. Or, ds.Table(0).TableName = "Orders" then use ds.Tables("Orders").DefaultView.

Monday, March 26, 2012

Object reference not set to an instance of an object

Hi: I have a problem, the error message is:

Object reference not set to an instance of an object

If GetDataFromTable(strMyTableName, GridView1)Then....End IfProtected Function GetDataFromTable(ByVal strMyTableNameAs String,ByRef gvMyGridViewAs GridView)As Boolean Try Dim dsAs New Data.DataSet'DataSetDim daAs OleDbDataAdapterDim strSQLAs String Dim tempRowAs Data.DataRowDim contblRepAs OleDbConnectionDim strConnAs String Dim strTypeAs String Dim intRepAs Int32Dim intRowsAs Int32 strConn = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString strSQL ="Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName contblRep =New OleDbConnection(strConn) da =New OleDbDataAdapter(strSQL, contblRep) da.Fill(ds)'fill the ds from da ds.Tables(0).Columns.Add("imgFile")'add extra column intRows = ds.Tables(0).Rows.Count.ToString()'get the total rows in the tables GridView1.DataSource = ds GridView1.DataBind() contblRep.Close()Return True'Catch ex As ExceptionCatch exAs ExceptionDim lblAs New LabelDim strMsgAs String = ex.Message lbl.Text ="<script language='javascript'>" & Environment.NewLine _ & "window.alert(" & "'" & strMsg & "'" & ")</script>" Page.Controls.Add(lbl)Return False End TryEnd Function

I don't mean you help me to read the code line by line, will be great if you point out why the error happens. The error message doesn't show on my local machine using ASP Express, but if I upload to a web hosting server, it pops up.

Thanks a lot.

Jt

Have you tried stripping the code out of the function, and just populating the Gridview, using the code (if so, does that work)?

What line does the error fall on?


Hi: Augustwind

Thanks for your suggestion, this is what I found:

Line1 strSQL = "Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName

Line2 strConn = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

Line3 contblRep = New OleDbConnection(strConn)

I found the Line2 Line3 will cause the Erro msg,

But If I write:

strSQL = "Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName

contblRep = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NewPandaDB.mdb")

Works find, that means: ConfigurationManager.ConnectionStrings is not working in my web hosting server. (It works here in my Pc, I am using ASP2.0 Express, OS is XP). I have no idea why. So far, I have to hard code the line: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NewPandaDB.mdb, I have to find a better way.

If you have any suggestion, please let me know. thanks.


I think the Namespace System.Configuration is missing:

try:

System.Configuration.ConfigurationManager.ConnectionStrings("myConnectionString").ToString

Chris


Hi,

I think you are missing the entry in Web.Config for the following key: "myConnectionString" in ConnectionStrings section. Are you just copy pasting the pages you need on the web server? Try using Publishing the WebSite. To 'Publish', right click on the Web Site in Solution Explorer and select 'Publish'.


Hi: Chris and Sujitm

Thanks for your suggestions, I did both, it works fine now. I am not exactly sure which one works for me, may be combine both.

Cheers

Jt

Thursday, March 22, 2012

Object reference not set to an instance of an object.

I list the code below, but the line that has the error is:
txtCompanyName.Enabled = false;

The error is:
Object reference not set to an instance of an object.

Default.aspx.cs

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;

public partial class _Default : System.Web.UI.Page
{
protected System.Web.UI.UserControl Companies;
protected System.Web.UI.WebControls.TextBox txtCompanyName;
protected System.Web.UI.WebControls.TextBox txtUniqueCode;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
txtCompanyName.Enabled = false;
txtUniqueCode.Enabled = false;
}

--------------------------
Companies.aspx.cs

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 partial class Companies : System.Web.UI.UserControl
{

}

--------------------------
Companies.ascx

<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true"
CodeFile="Companies.ascx.cs" Inherits="Companies" %>
<table style="width: 348px"
<caption style="color: #ffffff; background-color: #6666cc;
font-weight: bold; font-family: Arial;" align="center">
New Client Information</caption
<tr>
<td style="width: 544px; font-family: Arial; height: 6px;
background-color: buttonface;" align="right" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:Label ID="lblCompanyName" runat="server" Text="Company
Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px; height: 6px;" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:TextBox ID="txtCompanyName" runat="server"
CausesValidation="True" Width="220px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Must Enter Company Name"
ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
style="height: 6px; width: 3px;" bordercolor="#ffffff"
<tr>
<td style="width: 544px; font-family: Arial; background-color:
buttonface; height: 6px;" align="right" bgcolor="buttonface">
<asp:Label ID="lblUniqueCode" runat="server" Text="Unique
Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px" bgcolor="buttonface">
<asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
Width="220px"></asp:TextBox></td>
</tr
</table
Does it matter that the textboxes are only defined in the html code?
What do I need?

Thanks,
ripripA quick glance reveals that your text boxes are part of a user control and
you are attempting the examing them in another page. The control is only
available in the user control form.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-----------------

<aarepasky@.yahoo.com> wrote in message
news:1148340693.800290.207700@.j33g2000cwa.googlegr oups.com...
>I list the code below, but the line that has the error is:
> txtCompanyName.Enabled = false;
> The error is:
> Object reference not set to an instance of an object.
> Default.aspx.cs
> 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;
> public partial class _Default : System.Web.UI.Page
> {
> protected System.Web.UI.UserControl Companies;
> protected System.Web.UI.WebControls.TextBox txtCompanyName;
> protected System.Web.UI.WebControls.TextBox txtUniqueCode;
> protected void Page_Load(object sender, EventArgs e)
> {
> }
> protected void Button1_Click(object sender, EventArgs e)
> {
> txtCompanyName.Enabled = false;
> txtUniqueCode.Enabled = false;
> }
> --------------------------
> Companies.aspx.cs
> 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 partial class Companies : System.Web.UI.UserControl
> {
> }
> --------------------------
> Companies.ascx
> <%@. Control Language="C#" AutoEventWireup="true"
> CodeFile="Companies.ascx.cs" Inherits="Companies" %>
> <table style="width: 348px">
> <caption style="color: #ffffff; background-color: #6666cc;
> font-weight: bold; font-family: Arial;" align="center">
> New Client Information</caption>
> <tr>
> <td style="width: 544px; font-family: Arial; height: 6px;
> background-color: buttonface;" align="right" bordercolor="#ffffff"
> bgcolor="buttonface">
> <asp:Label ID="lblCompanyName" runat="server" Text="Company
> Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
> <td style="width: 318px; height: 6px;" bordercolor="#ffffff"
> bgcolor="buttonface">
> <asp:TextBox ID="txtCompanyName" runat="server"
> CausesValidation="True" Width="220px"></asp:TextBox></td>
> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
> runat="server" ErrorMessage="Must Enter Company Name"
> ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
> style="height: 6px; width: 3px;" bordercolor="#ffffff">
> <tr>
> <td style="width: 544px; font-family: Arial; background-color:
> buttonface; height: 6px;" align="right" bgcolor="buttonface">
> <asp:Label ID="lblUniqueCode" runat="server" Text="Unique
> Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
> <td style="width: 318px" bgcolor="buttonface">
> <asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
> Width="220px"></asp:TextBox></td>
> </tr>
> </table>
> Does it matter that the textboxes are only defined in the html code?
> What do I need?
> Thanks,
> riprip

Friday, March 16, 2012

Object reference not set to an instance of an object.

I list the code below, but the line that has the error is:
txtCompanyName.Enabled = false;
The error is:
Object reference not set to an instance of an object.
Default.aspx.cs
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;
public partial class _Default : System.Web.UI.Page
{
protected System.Web.UI.UserControl Companies;
protected System.Web.UI.WebControls.TextBox txtCompanyName;
protected System.Web.UI.WebControls.TextBox txtUniqueCode;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
txtCompanyName.Enabled = false;
txtUniqueCode.Enabled = false;
}
----
--
Companies.aspx.cs
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 partial class Companies : System.Web.UI.UserControl
{
}
----
--
Companies.ascx
<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true"
CodeFile="Companies.ascx.cs" Inherits="Companies" %>
<table style="width: 348px">
<caption style="color: #ffffff; background-color: #6666cc;
font-weight: bold; font-family: Arial;" align="center">
New Client Information</caption>
<tr>
<td style="width: 544px; font-family: Arial; height: 6px;
background-color: buttonface;" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:Label ID="lblCompanyName" runat="server" Text="Company
Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px; height: 6px;" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:TextBox ID="txtCompanyName" runat="server"
CausesValidation="True" Width="220px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Must Enter Company Name"
ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
style="height: 6px; width: 3px;" bordercolor="#ffffff">
<tr>
<td style="width: 544px; font-family: Arial; background-color:
buttonface; height: 6px;" bgcolor="buttonface">
<asp:Label ID="lblUniqueCode" runat="server" Text="Unique
Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px" bgcolor="buttonface">
<asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
Width="220px"></asp:TextBox></td>
</tr>
</table>
Does it matter that the textboxes are only defined in the html code?
What do I need?
Thanks,
ripripA quick glance reveals that your text boxes are part of a user control and
you are attempting the examing them in another page. The control is only
available in the user control form.
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
----
<aarepasky@.yahoo.com> wrote in message
news:1148340693.800290.207700@.j33g2000cwa.googlegroups.com...
>I list the code below, but the line that has the error is:
> txtCompanyName.Enabled = false;
> The error is:
> Object reference not set to an instance of an object.
> Default.aspx.cs
> 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;
> public partial class _Default : System.Web.UI.Page
> {
> protected System.Web.UI.UserControl Companies;
> protected System.Web.UI.WebControls.TextBox txtCompanyName;
> protected System.Web.UI.WebControls.TextBox txtUniqueCode;
> protected void Page_Load(object sender, EventArgs e)
> {
> }
> protected void Button1_Click(object sender, EventArgs e)
> {
> txtCompanyName.Enabled = false;
> txtUniqueCode.Enabled = false;
> }
> ----
--
> Companies.aspx.cs
> 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 partial class Companies : System.Web.UI.UserControl
> {
> }
> ----
--
> Companies.ascx
> <%@. Control Language="C#" AutoEventWireup="true"
> CodeFile="Companies.ascx.cs" Inherits="Companies" %>
> <table style="width: 348px">
> <caption style="color: #ffffff; background-color: #6666cc;
> font-weight: bold; font-family: Arial;" align="center">
> New Client Information</caption>
> <tr>
> <td style="width: 544px; font-family: Arial; height: 6px;
> background-color: buttonface;" bordercolor="#ffffff"
> bgcolor="buttonface">
> <asp:Label ID="lblCompanyName" runat="server" Text="Company
> Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
> <td style="width: 318px; height: 6px;" bordercolor="#ffffff"
> bgcolor="buttonface">
> <asp:TextBox ID="txtCompanyName" runat="server"
> CausesValidation="True" Width="220px"></asp:TextBox></td>
> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
> runat="server" ErrorMessage="Must Enter Company Name"
> ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
> style="height: 6px; width: 3px;" bordercolor="#ffffff">
> <tr>
> <td style="width: 544px; font-family: Arial; background-color:
> buttonface; height: 6px;" bgcolor="buttonface">
> <asp:Label ID="lblUniqueCode" runat="server" Text="Unique
> Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
> <td style="width: 318px" bgcolor="buttonface">
> <asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
> Width="220px"></asp:TextBox></td>
> </tr>
> </table>
> Does it matter that the textboxes are only defined in the html code?
> What do I need?
> Thanks,
> riprip
>