Fórmulas e Cenas

Object Reference Not Set to Instance of an Object

  Home  |   Contact  |   Syndication    |   Login
  20 Posts | 0 Stories | 1 Comments | 0 Trackbacks

News

Archives

Post Categories

Links

Friday, February 05, 2010 #

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=\"FileName.pdf\"");

Response.BinaryWrite(yourPdfAsByteArray);

Response.Flush();

Response.End();

Source

Enjoy :)


Tuesday, December 15, 2009 #

As found in: http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=15&t=17

 

public static Encoding GetFileEncoding(String FileName)
{
            Encoding Result = null;
            FileInfo FI = new FileInfo(FileName);
            FileStream FS = null;

            try
            {
                FS = FI.OpenRead();
                Encoding[] UnicodeEncodings = { Encoding.BigEndianUnicode, Encoding.Unicode, Encoding.UTF8 };
                for(int i = 0; Result == null && i < UnicodeEncodings.Length; i++)
                {
                    FS.Position = 0;
                    byte[] Preamble = UnicodeEncodings[i].GetPreamble();
                    bool PreamblesAreEqual = true;
                    for(int j = 0; PreamblesAreEqual && j < Preamble.Length; j++)
                    {
                        PreamblesAreEqual = Preamble[j] == FS.ReadByte();
                    }
                    if(PreamblesAreEqual)
                    {
                        Result = UnicodeEncodings[i];
                    }
                }
            }
            catch(System.IO.IOException)
            {
            }
            finally
            {
                if(FS != null)
                {
                    FS.Close();
                }
            }

            if(Result == null)
            {
                Result = Encoding.Default;
            }

            return Result;
        }

 

To use this:

 

 Encoding encode = GetFileEncoding(@"C:\myFile.txt");
 StreamReader re = new StreamReader(@"C:\myFile.txt", encode);

 

 

Enjoy :)


Monday, December 14, 2009 #

In this category I will post some "creative" solutions to problems. (Code changed to protect the inocents).

 

// The WTF way

int  myInt = Int32.Parse(myDataTable.Rows.Count + "");

// The correct way

int  myInt = myDataTable.Rows.Count;


Wednesday, November 25, 2009 #

Directory.CreateDirectory(@"c:\myNonExistingFolder\someNonExistingSubFolder\myFolderTocreate");

 

Enjoy ;)


Tuesday, November 24, 2009 #

myComboBox.DataSource = Enum.GetNames(typeof(myEnum));

 

Enjoy ;)


Thursday, October 29, 2009 #

Hello,

If you are reading this you probably have already realized that it is not possible to hide a column that was auto-generated using myGridView.Columns[index].Visible=false;
To achieve this you must:
1 - set the OnRowCreated to execute a function, something like OnRowCreated="gridRowCreated"
2 - Create your function as such:

protected void gridRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[yourIndexHere].Visible = false;
        }

Hope this helps ;)

Wednesday, October 28, 2009 #

public static WebProxy QueryIEProxySettings(string strFileURL)
{
    HttpWebRequest WebReqt = (HttpWebRequest)HttpWebRequest.Create(strFileURL);

    WebProxy WP = new WebProxy(WebReqt.Proxy.GetProxy(new Uri(strFileURL)));
    WP.Credentials = CredentialCache.DefaultCredentials;

    return WP; 

}


Tuesday, September 08, 2009 #

This post goes to all of you that are forced to cicle throught several projects on the course of the work day.

It comes handy to expand the recent project list so you don't have to look around.

To do this:
Menu Tool, Options, Environment, General. Inside this change the "items shown in recently used lists" to whatever suits your needs.

:)

Wednesday, August 19, 2009 #

A few days ago I got an error report to fix. Some web application that I now maintain was inserting duplicate records.

After a quick look it became obvious that the users where double clicking the submit button and that made the page post twice and so a duplicate record would appear.

A quick fix dor this problem using javascript:

<script language="javascript" type="text/javascript">
        var haveSubmitted=false;
        function FirstSubmitOnly() {
            if (haveSubmitted)
                return false;

            haveSubmitted = true;
            return true;
        }
    </script>


And at the form tag just invoke the function <form id="form1" runat="server" onsubmit="return FirstSubmitOnly();">



Enjoy :)

Wednesday, July 01, 2009 #

People keep asking how to cicle every enum term, so here is a post to explain ;)



foreach (OurEnum item in Enum.GetValues(typeof(OurEnum)))
{
      /* Code */
}