Hello,
I am using WebClient to upload some file to a server using the WebCLient.Uploadfile method
every time i try to run that method its throwing me a bad request error
The remote server returned an error: (400) Bad Request. is the exact exception message.
i dont know where am i missing. everything seems to be ok
can anybody help me on this
i ll appreciate the Help.
Thanks,
Lisa
hariomPosted Aug 30, 2010, 8:37 AM
Hi lisa use this code to upload ur file. This code import an Excel sheet data into database.
FileInfo fi = null;
string result = string.Empty;
DBAccess da = null;
DbConnection dbCon = null;
DbCommand dbCom = null;
DBParams[] dbParam = null;
ReadXML rxml = null;
DataSet ds = null;
try
{
if (flUpload.HasFile)
{
fi = new FileInfo(flUpload.FileName);
if (!fi.Extension.Contains("XLS") && !fi.Extension.Contains("xls") && !fi.Extension.Contains("xlsx"))
{
lblMessage.Text = "Invalid file selected.";
return;
}
try
{
flUpload.SaveAs(AppDomain.CurrentDomain.BaseDirectory + @"Upload\Excel\" + flUpload.FileName);
}
catch (System.UnauthorizedAccessException ex)
{
Common.ShowMessage(true, ex.Message, lblMessage);
LogInfo.GetInstance.WriteTextLog(ex.Message, LogInfo.LogType.Error, "WebUI_SoftTex_frmInvoiceDetUpload.btnUpload_Click");
return;
}
DataTable dtExcel = Common.readExcelFile(flUpload.FileName, "RevReport");
if (dtExcel.Rows.Count <= 0)
{
lblMessage.Text = "No records found in excel file.";
return;
}
else
{
string ConvRate=string.Empty;
for (int i = 0; i < dtExcel.Rows.Count ; i++)
{
ConvRate = dtExcel.Rows[i][41].ToString();
if (ConvRate == "")
{
lblMessage.Text = "There is a invalid entry for Conversion Rate in the excel file.";
return;
}
}
ChangeColHeader(ref dtExcel);
using (StringWriter sw = new StringWriter())
{
dtExcel.WriteXml(sw);
result = sw.ToString();
try
{
da = new DBAccess();
object[] _objVal = new object[6];
_objVal[0] = result;
_objVal[1] = base.pUserId;
_objVal[2] = 0;
_objVal[3] = string.Empty;
_objVal[4] = 0;
_objVal[5] = 0;
rxml = new ReadXML();
dbParam = rxml.getParameters("ZSTSP_UPLOAD_INVOICE_DET", 6, _objVal);
dbCon = da.InitConnection(DAO.ConnectTo.SQLCon);
dbCom = da.InitDbCommand(dbCon, "ZSTSP_UPLOAD_INVOICE_DET", dbParam);
ds = da.GetData(dbCom);
if (dbCom.Parameters[3].Value.ToString().Length <= 0)
{
lblSucRecoCount.Text = dbCom.Parameters[4].Value.ToString();
lblErrRecoCount.Text = dbCom.Parameters[5].Value.ToString();
downloadFile(ds);
btnUpload.Enabled = false;
trConfirm.Visible = true;
btnOK.Visible = false;
}
else
{
lblMessage.Text = dbCom.Parameters[3].Value.ToString();
}
}
catch (Exception ex)
{
Common.ShowMessage(true, ex.Message, lblMessage);
LogInfo.GetInstance.WriteTextLog(ex.Message, LogInfo.LogType.Error, "WebUI_SoftTex_frmInvoiceDetUpload:ZSTSP_UPLOAD_INVOICE_DET()");
throw ex;
}
}
}
}
else
{
lblMessage.Text = "Please select the excel file.";
return;
}
}
catch (Exception ex)
{
LogInfo.GetInstance.WriteTextLog(ex.Message, LogInfo.LogType.Error, "InvoiceDetUpload:btnUpload_Click");
lblMessage.Text = ex.Message;
}
finally
{
fi = null;
if (da != null)
da.Dispose();
dbCon = null;
dbCom = null;
dbParam = null;
rxml = null;
ds = null;
}
LisaPosted Feb 17, 2009, 7:56 AM