Hi guys, I just want to ask some help

I added a Download Button in every row of my Gridview. Its function is to download the Binary File of the corresponding row.

Whenever i run it it gives me the error "Object reference not set to an instance of an object."

Pls help me im just a beginner @_@

Heres my code behind:

protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {

            String[] fileInfo = e.CommandArgument.ToString().Split(new char[] { ',' });

            int DocID = Int32.Parse(fileInfo[0]);
            String FileName = fileInfo[0];

            SqlConnection conn = new SqlConnection("My connection String");

            SqlCommand cmd = new SqlCommand("SELECT BinaryData from BinaryTable WHERE DocID = @DocID");

            cmd.Parameters.AddWithValue("@DocID", DocID);
            cmd.Connection = conn;
            conn.Open();


            byte[] BinaryData = (Byte[])cmd.ExecuteScalar();


            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" +   FileName + ".zip");
            Response.BinaryWrite(BinaryData);

        }

    }
}

Any help would be highly appreciated. Thanks!
by the way, im using visual studio 2005 and sql server 2005

I suspect the error is coming from either of e.CommandName line or from e.CommandArgument.ToString() line.

Just let me know one thing, did you set the CommandName and CommandArgument property of your Download command button in gridview.

Also why are you using Split( ) function ?

Hi guys, I just want to ask some help

I added a Download Button in every row of my Gridview. Its function is to download the Binary File of the corresponding row.

Whenever i run it it gives me the error "Object reference not set to an instance of an object."

Pls help me im just a beginner @_@

Heres my code behind:

    protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {

            String[] fileInfo = e.CommandArgument.ToString().Split(new char[] { ',' });

            int DocID = Int32.Parse(fileInfo[0]);
            String FileName = fileInfo[0];

            SqlConnection conn = new SqlConnection("My connection String");

            SqlCommand cmd = new SqlCommand("SELECT BinaryData from BinaryTable WHERE DocID = @DocID");

            cmd.Parameters.AddWithValue("@DocID", DocID);
            cmd.Connection = conn;
            conn.Open();


            byte[] BinaryData = (Byte[])cmd.ExecuteScalar();


            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" +   FileName + ".zip");
            Response.BinaryWrite(BinaryData);

        }

    }
}

Any help would be highly appreciated. Thanks!
by the way, im using visual studio 2005 and sql server 2005

I suspect the error is coming from either of e.CommandName line or from e.CommandArgument.ToString() line.

Just let me know one thing, did you set the CommandName and CommandArgument property of your Download command button in gridview.

Also why are you using Split( ) function ?

Yes i did.
Honestly, i dont know i just saw that on a tutorial and followed it.

I have no clue why it is happening since im only starting with this @_@

Just let me know one thing, do you have set multiple value separated by comma (,) in CommandArgument property of Download button ?

In below line you are converting " fileInfo[0]" to Integer as well string. So the question is is it string or integer.

int DocID = Int32.Parse(fileInfo[0]);
String FileName = fileInfo[0];

frankly speaking if you want to be a good programmer then when you refere any tutorial just try to understand the concept first and then analyze the code and then use.. and please don't take it personnaly..

Yes i did.
Honestly, i dont know i just saw that on a tutorial and followed it.

I have no clue why it is happening since im only starting with this @_@

Just let me know one thing, do you have set multiple value separated by comma (,) in CommandArgument property of Download button ?

In below line you are converting " fileInfo[0]" to Integer as well string. So the question is is it string or integer.

int DocID = Int32.Parse(fileInfo[0]);
String FileName = fileInfo[0];

frankly speaking if you want to be a good programmer then when you refere any tutorial just try to understand the concept first and then analyze the code and then use.. and please don't take it personnaly..

No i dont think so. And by the way where can i find that command argument property? i cant seem to find it on the edit column tag of the gridview..

and yes i do know that. the problem is im only trying this method and still do not know the process of this. Basically i need more familiarization. No problem buddy :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.