Fórmulas e Cenas

Object Reference Not Set to Instance of an Object

  Home  |   Contact  |   Syndication    |   Login
  39 Posts | 0 Stories | 6 Comments | 0 Trackbacks

News

Archives

Post Categories

Links

Monday, December 19, 2011 #

If you have a "{" or "}" inside a string when using string.format you will get the error mentioned in the title.

To fix this you must double the charater.

Error Example: 
string teste = string.Format(" This is a {teste} {0} ", mystring);

Correct Example:
string teste = string.Format(" This is a {{teste}} {0} ", mystring);v
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Tuesday, September 20, 2011 #

In global.asax:

 

protected void Application_BeginRequest(Object sender, EventArgs e)
{
	HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
	//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
	int maxRequestLength = (runTime.MaxRequestLength - 100* 1024;
 
	//This code is used to check the request length of the page and if the request length is greater than 
	//MaxRequestLength then retrun to the same page with extra query string value action=exception

	HttpContext context = ((HttpApplication)sender).Context;
	if(context.Request.ContentLength > maxRequestLength)
	{
		IServiceProvider provider = (IServiceProvider)context;
		HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
		// Check if body contains data
		if(workerRequest.HasEntityBody())
		{
			// get the total body length
			int requestLength = workerRequest.GetTotalEntityBodyLength();
			// Get the initial bytes loaded
			int initialBytes = 0;
			if(workerRequest.GetPreloadedEntityBody() != null)
				initialBytes = workerRequest.GetPreloadedEntityBody().Length;
			if(!workerRequest.IsEntireEntityBodyIsPreloaded())
			{
				byte[] buffer = new byte[512000];
				// Set the received bytes to initial bytes before start reading
				int receivedBytes = initialBytes;
				while(requestLength - receivedBytes >= initialBytes)
				{
					// Read another set of bytes
					initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
					// Update the received bytes
					receivedBytes += initialBytes;
				}
				initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
			}
		}
		// Redirect the user to the same page with querystring action=exception. 
		context.Response.Redirect("~/Erros/FicheiroGrande.aspx");
	}
}

 

Source
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

<script type="text/javascript" language="javascript">

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(refresh);

        function refresh() { //Your code here; }

</script>

 

Source

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, June 16, 2011 #

/* Processes that are blocking others */
select * from 
sysprocesses
where spid in
(
select 
	blocked
from 
	syslocks l
	inner join sysprocesses p on p.spid=l.spid
where 
	p.dbid=5 -- Database ID
	and p.blocked<>0
)

/* Processes being blocked*/
select 
	p.*
from 
	syslocks l
	inner join sysprocesses p on p.spid=l.spid
where 
	p.dbid=5
	and p.blocked<>0

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, October 07, 2010 #

 FileInfo fileInf = new FileInfo("c:\myfile.txt");

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPSite/myfile.txt"));

reqFTP.Credentials = new NetworkCredential("myUser","myPassword", "myFTPDomain");
 

reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();

try
{
    Stream strm = reqFTP.GetRequestStream();
    contentLen = fs.Read(buff, 0, buffLength);
    while (contentLen != 0)
    {
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }
    strm.Close();
    fs.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

 

Enjoy :)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

FileStream outputStream = new FileStream("c:\myfilefolder\file.txt");
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPSite/file.txt"));

reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("myUser","MyPassword","FTPDomain");

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();

int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
    outputStream.Write(buffer, 0, readCount);
    readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
outputStream.Close();
response.Close();

 

Enjoy :)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPURL/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("myUser", "myPassword", "FTPDomain");

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while (line != null)
{
    line = reader.ReadLine();
}

reader.Close();
response.Close();

 

Enjoy :)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Tuesday, August 17, 2010 #

The scenario:

Imagine someone asks you to create an XML file with some data structure.

The approuch:

There are several ways to achieve this, the fastest and less time comsuming is to create a class structure that represents the XML structure you need, fill in with the data and them serialize it to a string and save it to a file.

The Implementation:

Lets say you need a single node, followed by an array of nodes.

Class Definition:

public class MyNode
{

[XmlElementAttribute("code")]
public int code;
[XmlElementAttribute("description")]
public string description;

}

 

public class Item
{

[XmlAttribute("name")]
public string name;
[XmlAttribute("value")]
public string value;

}

 

[XmlRootAttribute("myClass", IsNullable = false, Namespace = "")]
public class MyClass
{

public MyClass()
{
myNode = new MyNode();
}

 

[XmlElementAttribute("myNode")]
public MyNode myNode;

 

[XmlArrayAttribute("myArray")]
public List<Item> myArray = new List<Item>();

 

public override string ToString()
{
try
{
string xmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(MyClass));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, this);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlString = UTF8ByteArrayToString(memoryStream.ToArray());
return xmlString;
}
catch
{
return string.Empty;
}
}

 

private static string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}

 

private static Byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}

 

public static MyClass DeserializeObject(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(MyClass));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return (MyClass)xs.Deserialize(memoryStream);
}
 

}
 

Now, to use these classes:

public static string myFunction()
{

MyClass fido = new MyClass();
fido.myNode.code = 1;
fido.myNode.description="sample text";
myArray.Add(new Item{name="name1", value="value1"});
myArray.Add(new Item{name="name2", value="value2"});
return fido.ToString();

}

Executing this you will get an string as such:

<?xml version="1.0" encoding="utf-8"?>
<myClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<myNode>
<code>1</code>
<description>sample text</description>
</myNode>
<myArray>
<Item name="name1" value="value1" />
<Item name="name2" value="value2" />
</myArray>

</myClass>

Using the string returned from "fido.ToString()" in the method DeserializeObject will get you the original values for the object.

Hope this can be helpfull,

 

Enjoy :)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Wednesday, August 11, 2010 #

Occasionally an application that I am working on throws the above error... 

After trying to fix my code for a bit, and not finding the problem I googled a bit and discovered that this error is caused by a windows service that must be running.

So, if you ever encounter this error, you must turn on the 'Distributed Transaction Coordinator' in order to solve it.

Hope this helps.


Original source: http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Friday, July 09, 2010 #

update n
set
    n.fieldToUpdate=o.fieldToUpdateFrom
from
   originaltable o 
   inner join toTable n on n.id= o.id
 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati