Fórmulas e Cenas

Object Reference Not Set to an Instance of an Object

  Home  |   Contact  |   Syndication    |   Login
  44 Posts | 0 Stories | 13 Comments | 0 Trackbacks

News

Archives

Post Categories

Links

C#

When you are inserting into a table using entity framework, on rare occasions you might get the following error: {System.Data.OptimisticConc... Store update, insert, or delete statement affected an unexpected number of rows (0) This happened to me because of an "instead of INSERT" trigger that was active on the page. It turns out that EF does not go well with triggers, because it fails to get the inserted ID. For me, the solution was to change the trigger so that the last executed code ......

DataView view = new DataView(table);DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...); Source: http://stackoverflow.com/qu... ......

Simple and direct:

myContainer.myEntity.Where("it.myField LIKE '%' + @searchTerm + '%'", new ObjectParameter("searchTerm", myQueryText));


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 ......

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

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

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

</script>

 

Source


FileInfo fileInf = new FileInfo("c:\myfile.txt"); FtpWebRequest reqFTP = (FtpWebRequest)FtpWebReques... Uri("ftp://myFTPSite/myfile... reqFTP.Credentials = new NetworkCredential("myUser",... "myFTPDomain"); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.Uploa... reqFTP.UseBinary = true; reqFTP.ContentLength = fileInf.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInf.OpenRead(); try { Stream strm = ......

FileStream outputStream = new FileStream("c:\myfilefolder... FtpWebRequest reqFTP = (FtpWebRequest)FtpWebReques... Uri("ftp://myFTPSite/file.t... reqFTP.Method = WebRequestMethods.Ftp.Downl... reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential("myUser",... FtpWebResponse response = (FtpWebResponse)reqFTP.GetR... Stream ftpStream = response.GetResponseStream(); int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; ......

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebReques... Uri("ftp://myFTPURL/")); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential("myUser", "myPassword", "FTPDomain"); reqFTP.Method = WebRequestMethods.Ftp.ListD... WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetRe... string line = reader.ReadLine(); while (line != null) { line = reader.ReadLine(); } reader.Close(); response.Close(); Enjoy :) ......

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; ......

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/n... ......

Full C# Archive