Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Monday, March 26, 2012

Object reference not set to an instance of an object

I am receiving the above error when I try to read a database object. My connection string is : "server=JIMM;database=MIST01;Trusted_Connection=yes". I have added user JIMM\ASPNET to the database with public, db_datareader, and db_datawriter privileges. Permisssions have been selected for Select, Insert, Update, and Delete for all user defined objects. What am I missing? I can open the database connection, but it fails on an ExecuteScalar command.Can you provide us with some more code since the error can be in a few places...?
Most of the code is in the following snippet from the data access layer. The Read_ID function is called from the business logic layer. Read_ID uses the Connection String member and it calls the ExecuteScalar function. The error occurs when "return (int)cmd.ExecuteScalar();" is executed. I'm assuming that it is a permission error, but I just can't see it. Thanks for your help.

public class DataConfig
{
public DataConfig()
{

}
private static int _id;
private static string _ConnectString;
public static int IdentitySeed
{
get
{
return _id*1000000;
}
}
public static string ConnectionString
{
get
{
return _ConnectString;
}
set
{
_ConnectString = value;
}
}
public static int ReadID(string serverName)
{
// read inspector table to access identity seed

SqlCommand readID = new SqlCommand("SELECT InspectorID FROM Inspectors WHERE (HostName = '" + serverName + "')");

try
{
_id = (int) ExecuteScalar(readID);
return _id;
}
catch (Exception ex)
{
throw ex;
}
}
public static int ExecuteSQL(SqlCommand cmd)
{

try
{
cmd.Connection = new SqlConnection(DataConfig.ConnectionString);
cmd.Connection.Open();

return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd.Connection != null)
{
if (cmd.Connection.State != ConnectionState.Closed)
{
cmd.Connection.Close();
}
}
}
}
public static int ExecuteScalar(SqlCommand cmd)
{

try
{
cmd.Connection = new SqlConnection(DataConfig.ConnectionString);
cmd.Connection.Open();

return (int)cmd.ExecuteScalar();
}
catch (InvalidCastException ex)
{
return 0;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd.Connection != null)
{
if (cmd.Connection.State != ConnectionState.Closed)
{
cmd.Connection.Close();
}
}
}
}

Saturday, March 24, 2012

Object reference not set to an instance of an object

On a webform, I made a literal DateRange and in Page_Load() assigned to its text a string strDates which I filled with HTML code for 2 date dropdownlist When I ran the form the above error popped up.

The line where the error occurred is :

Me.DatetRange.Text = strDates

What's wrong?

Thanks for any help.Hi,

I'm not sure about your situation, but usually when i get that error there's a word or lettering someplace thats wrong. It's usually not a big deal but i've spent hours before searching for what might be wrong and it turned out to be an s on the end of a work that shouldnt have been there or something dumb like that.

I would check your html and your code behind very carefully.

Hope you work it out
The error states that Me.DatetRange was not initialized. Shouldn't it be Me.DateRange?
I corrected it to DateRange and still get the error.
You don't need to use 'Me.' when referring to a literal, if you're thinking of 'Me' as being the form or page...

if DateRange is the ID of the Literal - - just use DateRange.Text=strDates (depending, of course, on the correct spelling)

Thursday, March 22, 2012

Object reference not set to an instance of an object HELP!

i get this server error when i try to run httpwriter. Can anyone help? i'm trying to run a string of html codings to open in current/new window. Not sure if this is able to work though..

Object reference not set to an instance of an object

Line 151: HTP.Write(x);

C# file

part of CODE:

publicclass test2 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button BTNClick;

protectedSystem.Web.HttpWriter Writer;

}

...

privatevoid BTNClick_Click(object sender, System.EventArgs e)

{

string x =@dotnet.itags.org."...ssssss"

Writer.write(x);

}

...

i get this error sometimes when i do not create an instance from the class for example

Dim a as ClassName()

a.print()

this is where i have problem!!!

but if you do

Dim a as New ClassName()

a.print()

That will be fine as i have created new instance by saying "New" key word... but notice that this message error is not always mean this !!! so try that and let me know !


This does not create an HttpWriter
protected System.Web.HttpWriter Writer;
It only declares one. In fact, just do this as you've already got an instance of an HttpWriter built into the Page class.

private void BTNClick_Click(object sender, System.EventArgs e)
{
string x=@."...ssssss"

Response.Write(x);
}

NC...

Object reference not set to an instance of an object.

Dim sTextBoxName As String =
e.Item.Cells(0).FindControl("tbtempDateF").ClientID() ******Error Here

CType(e.Item.FindControl("lnkCalendarF"), HyperLink).NavigateUrl =
"javascript:calendar_window=window.open('calendar.a spx?formname=Form1." &
sTextBoxName &
"','Pick_A_Date','width=154,height=210');calendar_w indow.focus();"

--
I am using the free version of SPAMfighter for private users.
It has removed 0 spam emails to date.
Paying users do not have this message in their emails.
Try www.SPAMfighter.com for free now!Clearly, something in that chain is coming up with a null reference. My
guess is that it cannot find the control you are telling it to via
FindControl.

"Fetty" <dfetrow410@.hotmail.com> wrote in message
news:%234MlSpxVFHA.3488@.tk2msftngp13.phx.gbl...
> Dim sTextBoxName As String =
> e.Item.Cells(0).FindControl("tbtempDateF").ClientID() ******Error Here
> CType(e.Item.FindControl("lnkCalendarF"), HyperLink).NavigateUrl =
> "javascript:calendar_window=window.open('calendar.a spx?formname=Form1." &
> sTextBoxName &
> "','Pick_A_Date','width=154,height=210');calendar_w indow.focus();"
>
> --
> I am using the free version of SPAMfighter for private users.
> It has removed 0 spam emails to date.
> Paying users do not have this message in their emails.
> Try www.SPAMfighter.com for free now!
probably..
Solved it?
Patrick

"Marina" wrote:

> Clearly, something in that chain is coming up with a null reference. My
> guess is that it cannot find the control you are telling it to via
> FindControl.
> "Fetty" <dfetrow410@.hotmail.com> wrote in message
> news:%234MlSpxVFHA.3488@.tk2msftngp13.phx.gbl...
> > Dim sTextBoxName As String =
> > e.Item.Cells(0).FindControl("tbtempDateF").ClientID() ******Error Here
> > CType(e.Item.FindControl("lnkCalendarF"), HyperLink).NavigateUrl =
> > "javascript:calendar_window=window.open('calendar.a spx?formname=Form1." &
> > sTextBoxName &
> > "','Pick_A_Date','width=154,height=210');calendar_w indow.focus();"
> > --
> > I am using the free version of SPAMfighter for private users.
> > It has removed 0 spam emails to date.
> > Paying users do not have this message in their emails.
> > Try www.SPAMfighter.com for free now!
>

Object reference not set to an instance of an object.

hi, help..

protected void Item_Click(object sender, DataGridCommandEventArgs e)
{
if(((LinkButton)e.CommandSource).CommandName == "SelectItem")
{
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

try
{
DataRow dr = ds1.tblPlasmaInfomation.FindByPlasmaid(Int32.Parse (key));
lbccuname.Text = dr["Plasma_ccuname"].ToString();
}
catch(Exception exc)
{
TextBox1.Text = exc.Message;
}
}

}

i step by step run..and dr always null...how can i sol this
problem..thanks..If you can step through it, you can do some Quick Watches as well. You need
to find out what you're getting back with the line of code that assigns a
value to the "key" variable, since obvioulsy you're not getting anything
back when you use it to find your DataRow.

--
HTH,

Kevin Spencer
..Net Developer
Microsoft MVP
http://www.takempis.com
Big things are made up
of lots of little things

"yysiow" <yysiow@.ytlesolutions.com> wrote in message
news:OAcRLlCSDHA.212@.TK2MSFTNGP10.phx.gbl...
> hi, help..
> protected void Item_Click(object sender, DataGridCommandEventArgs e)
> {
> if(((LinkButton)e.CommandSource).CommandName == "SelectItem")
> {
> string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
> try
> {
> DataRow dr =
ds1.tblPlasmaInfomation.FindByPlasmaid(Int32.Parse (key));
> lbccuname.Text = dr["Plasma_ccuname"].ToString();
> }
> catch(Exception exc)
> {
> TextBox1.Text = exc.Message;
> }
> }
> }
>
> i step by step run..and dr always null...how can i sol this
> problem..thanks..
If you can step through it, you can do some Quick Watches as well. You need
to find out what you're getting back with the line of code that assigns a
value to the "key" variable, since obvioulsy you're not getting anything
back when you use it to find your DataRow.

--
HTH,

Kevin Spencer
..Net Developer
Microsoft MVP
http://www.takempis.com
Big things are made up
of lots of little things

"yysiow" <yysiow@.ytlesolutions.com> wrote in message
news:OAcRLlCSDHA.212@.TK2MSFTNGP10.phx.gbl...
> hi, help..
> protected void Item_Click(object sender, DataGridCommandEventArgs e)
> {
> if(((LinkButton)e.CommandSource).CommandName == "SelectItem")
> {
> string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
> try
> {
> DataRow dr =
ds1.tblPlasmaInfomation.FindByPlasmaid(Int32.Parse (key));
> lbccuname.Text = dr["Plasma_ccuname"].ToString();
> }
> catch(Exception exc)
> {
> TextBox1.Text = exc.Message;
> }
> }
> }
>
> i step by step run..and dr always null...how can i sol this
> problem..thanks..

Friday, March 16, 2012

Object reference not set to an instance of an object.

Dim sTextBoxName As String =
e.Item.Cells(0).FindControl("tbtempDateF").ClientID() ******Error Here
CType(e.Item.FindControl("lnkCalendarF"), HyperLink).NavigateUrl =
"java script:calendar_window=window.open('calendar.aspx?formname=Form1." &
sTextBoxName &
" ','Pick_A_Date','width=154,height=210');
calendar_window.focus();"
I am using the free version of SPAMfighter for private users.
It has removed 0 spam emails to date.
Paying users do not have this message in their emails.
Try www.SPAMfighter.com for free now!Clearly, something in that chain is coming up with a null reference. My
guess is that it cannot find the control you are telling it to via
FindControl.
"Fetty" <dfetrow410@.hotmail.com> wrote in message
news:%234MlSpxVFHA.3488@.tk2msftngp13.phx.gbl...
> Dim sTextBoxName As String =
> e.Item.Cells(0).FindControl("tbtempDateF").ClientID() ******Error Here
> CType(e.Item.FindControl("lnkCalendarF"), HyperLink).NavigateUrl =
> "java script:calendar_window=window.open('calendar.aspx?formname=Form1." &
> sTextBoxName &
> " ','Pick_A_Date','width=154,height=210');
calendar_window.focus();"
>
> --
> I am using the free version of SPAMfighter for private users.
> It has removed 0 spam emails to date.
> Paying users do not have this message in their emails.
> Try www.SPAMfighter.com for free now!
>
probably..
Solved it?
Patrick
"Marina" wrote:

> Clearly, something in that chain is coming up with a null reference. My
> guess is that it cannot find the control you are telling it to via
> FindControl.
> "Fetty" <dfetrow410@.hotmail.com> wrote in message
> news:%234MlSpxVFHA.3488@.tk2msftngp13.phx.gbl...
>
>