upload documents not save in database

Posted by Diyaayeshaa under ASP.NET on 10/1/2013 | Points: 10 | Views : 1208 | Status : [Member] | Replies : 3
i try to upload documnets and save in database and in directory
but it saves in directory not able to save in database
here i upload code
if (FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength > 0)
{
if (FileUploadControl.FileContent.Length < 100000)
{
string filename = Path.GetFileName(FileUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Docfiles/");
Directory.CreateDirectory(folder);
FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
try
{


cc.fileupladdd(Txt_docde.Value, txt_dname.Value, FileUploadControl.FileName, Convert.ToInt32(Docdrop.SelectedValue), Convert.ToInt32(DepTypeee.SelectedValue), Convert.ToInt32(Session["UserID"]));
StatusLabel.ForeColor = System.Drawing.Color.Green;
StatusLabel.Text = "Success";
}
catch
{
StatusLabel.ForeColor = System.Drawing.Color.Red;
StatusLabel.Text = "Failed";
}
}
else
{
StatusLabel.ForeColor = System.Drawing.Color.Red;
StatusLabel.Text = "File Size to big";
}
}
Txt_docde.Value = "";
txt_dname.Value = "";
}
}




and here is


sp
ALTER procedure [dbo].[fileuplaod]
@DocDesciption nvarchar(50),
@DocName nvarchar(50),
@Uploadfile nvarchar(50),
@DocTypeID int,
@DepID int,
@UserID int
as
insert into DocumentInfo(DocDesciption ,DocName,Uploadfile,DocTypeID,DepID ,UserID)
values(@DocDesciption,@DocName,@Uploadfile,@DocTypeID,@DepID ,@UserID)


where is the problem ? please tell me




Responses

Posted by: Raj.Trivedi on: 10/1/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello,

I dont see that you have used your procedure anywhere in the logic.

You can modify the code as

if (FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength > 0)

{

if (FileUploadControl.FileContent.Length < 100000)

{

string filename = Path.GetFileName(FileUploadControl.PostedFile.FileName);

string folder = Server.MapPath("~/Docfiles/");

Directory.CreateDirectory(folder);

FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));

try

{





sqlconnection con = new sqlconnection("your connectionstring");
sqlcommand cmdinsert = new sqlcommand("YourStoreProcName",con);
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.parameters.add("@YourStoredProcParameter").value = YourControlId OR Variable ;
cmd.CommandType = CommandType.StoreProcedure;
int result = cmd.executenonquery();
if(result==1){StatusLabel.ForeColor = System.Drawing.Color.Green;

StatusLabel.Text = "Success";}
else
{
StatusLabel.ForeColor = System.Drawing.Color.Red;

StatusLabel.Text = "Failed";
}




}

catch

{

StatusLabel.ForeColor = System.Drawing.Color.Red;

StatusLabel.Text = "Failed";

}

}

else

{

StatusLabel.ForeColor = System.Drawing.Color.Red;

StatusLabel.Text = "File Size to big";

}

}

Txt_docde.Value = "";

txt_dname.Value = "";

}

}



Hope this helps

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Diyaayeshaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Diyaayeshaa on: 10/1/2013 [Member] Starter | Points: 25

Up
0
Down
i create a function and call store procedure in function then this function i cal in this code
function
 public void fileupladdd(string DocDesc, string Docname, string file, int doctypeid, int depid,int UserID)

{
db.ExecuteNonQuery("fileuplaod", new object[] { DocDesc, Docname, file, doctypeid, depid, UserID });


}



and when i debug the whole code it shw me error input string was not in correct format

Diyaayeshaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.Saravanan on: 10/1/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Could you please verify the following controls got values?
- Docdrop.SelectedValue
- DepTypeee.SelectedValue

If no, check this condition...
int nDocDrop = Docdrop.SelectedValue.Equals(string.Empty)? -1: Convert.ToInt32(Docdrop.SelectedValue.ToString());

int nDepTypeee = DepTypeee.SelectedValue.Equals(string.Empty)? -1: Convert.ToInt32(DepTypeee.SelectedValue.ToString());


Kindly replace the line with following code...
Directory.CreateDirectory(folder);

into
if(Directory.Exists(folder))  // To check the path is already exist or not

{
Directory.CreateDirectory(folder);
}


Thanks,
T.Saravanan

Diyaayeshaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response