String strConnectionString = "Driver={MySQL ODBC 3.51
Driver};Server=server;Database=kv;user id=kv;password=kv";
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();
}
}
my table
table name--- dg_admainfile
Description --VARCHAR(45)
Filename--VARCHAR(45)
Picture- LONGBLOB
Type -VARCHAR(45)
cid - INTEGER
pid --INTEGER
im getting following error
Object reference not set to an instance of an object.
plz anyone help me to rectify the error