Object reference not set to an instance of an object this is not all it shows you. Can you please specify closer the expection?
2009/6/24 nag <[email protected]> > > hii dear all, > im getting following error while uploading file to mysql database > > Object reference not set to an instance of an object > > here is my code please anyone help me to do the same > > <table align ="center" class="panel1" style="width: 99%; height: > 118px;"> > <tr> > <td style="width: 464px; height: 21px; text-align: > left;"> > Description: > <asp:TextBox ID="txtFileName" runat="server"></ > asp:TextBox></td> > </tr> > <tr> > > </tr> > <tr> > <td style="width: 464px; text-align: left;"> > Document: > <input id="File1" runat="server" type="file" > style="width: 266px" /></td> > </tr> > <tr> > <td style="width: 464px; height: 26px;"> > <asp:Button ID="Button1" runat="server" > Text="Upload" OnClick="Button1_Click" /></td> > </tr> > > </table> > > > C# code > > String strConnectionString = "Driver={MySQL ODBC 3.51 > Driver};Server=mysql8.brinkster.com;Database=dv;user > id=dv;password=bv"; > > protected void Button1_Click(object sender, EventArgs e) > { > FileInfo imageInfo = new FileInfo(File1.Value.Trim()); > //if (!imageInfo.Exists) > // this.RegisterClientScriptBlock("alertLabel1", > "<script>alert('please select one image file.');</script>"); > //else > //{ > switch (imageInfo.Extension.ToUpper()) > { > case ".JPG": this.UpLoadImageFile(imageInfo); break; > case ".GIF": this.UpLoadImageFile(imageInfo); break; > case ".BMP": this.UpLoadImageFile(imageInfo); break; > case ".ZIP": this.UpLoadImageFile(imageInfo); break; > case ".DOC": this.UpLoadImageFile(imageInfo); break; > case ".PDF": this.UpLoadImageFile(imageInfo); break; > case ".wmv": this.UpLoadImageFile(imageInfo); break; > default: this.RegisterClientScriptBlock("alertLabel1", > "<script>alert('file type error.');</script>"); break; > } > //} > } > private void UpLoadImageFile(FileInfo info) > { > OdbcConnection objConn = null; > OdbcCommand objCom = null; > try > { > byte[] content = new byte[info.Length]; > FileStream imagestream = info.OpenRead(); > imagestream.Read(content, 0, content.Length); > imagestream.Close(); > objConn = new OdbcConnection(strConnectionString); > objCom = new OdbcCommand("insert into dg_admainfile > (Description,Filename,Picture,Type,cid,pid)values(?,?,?,?,?,?)", > objConn); > OdbcParameter categorynameParameter = new OdbcParameter > ("@Description", OdbcType.NVarChar); > if (this.txtFileName.Text.Trim().Equals("")) > categorynameParameter.Value = "Default"; > else > objCom.Parameters.Add("@Filename", > OdbcType.NVarChar).Value = System.IO.Path.GetFileName > (File1.PostedFile.FileName); > objCom.Parameters.Add("@Type", OdbcType.NVarChar).Value = > System.IO.Path.GetFileName(File1.PostedFile.ContentType); > objCom.Parameters.Add("@cid", OdbcType.Int).Value = Session > ["clientid"].ToString(); > objCom.Parameters.Add("@pid", OdbcType.Int).Value = Session > ["projectid"].ToString(); > categorynameParameter.Value = this.txtFileName.Text.Trim > (); > objCom.Parameters.Add(categorynameParameter); > > OdbcParameter pictureParameter = new OdbcParameter > ("@Picture", OdbcType.Image); > pictureParameter.Value = content; > objCom.Parameters.Add(pictureParameter); > objConn.Open(); > objCom.ExecuteNonQuery(); > objConn.Close(); > } > catch (Exception ex) > { > throw new Exception(ex.Message); > } > finally > { > objConn.Close(); > } > } > > > table name : dg_clientfile > > Description --VARCHAR(45) > > Filename--VARCHAR(45) > Picture --LONGBLOB > Type ---VARCHAR(45) > > cid --INTEGER > > pid --INTEGER > > > >
