Value of type 'System.IO.DirectoryInfo' cannot be converted to 'String'.

I am uploading documents, and have a basic multiple file upload (from the .NET tutorial) working great.  BUT, instead of putting all the files uploaded via the webpage, I want to put them in sub folders.  So,  I want each file to go in the C:\ directory, in a folder named Uploaded Files, and then, a sub folder with the number of a hiddenfield that is hdn.MatterID...   Here's what I tried, but am getting the error,  Value of type 'System.IO.DirectoryInfo' cannot be converted to 'String'

code behind

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim uploads As HttpFileCollection

uploads = HttpContext.Current.Request.Files

For i As Integer = 0 To (uploads.Count - 1)

If (uploads(i).ContentLength > 0) Then

Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName) Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\ + hdnMatterID.Value\")

Try

uploads(i).SaveAs(d + c)

Span1.InnerHtml = "File Uploaded Sucessfully."

Catch Exp As Exception

Span1.InnerHtml = "Some Error occured."

End Try

End If

Next i

End Sub


Never make important decisions on a Monday!
0
funluckykitty
11/20/2008 7:28:34 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

8 Replies
1829 Views

Similar Articles

[PageSpeed] 26
Get it on Google Play
Get it on Apple App Store

funluckykitty:

Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\ + hdnMatterID.Value\")

CreateDirectory returns a type DirectoryInfo and you are trying to assign that to a string. That is why you are getting that exception. Are you using the variable "d" somewhere?


Thanks,
Max
Let Me Google That For You!
0
bullpit
11/20/2008 7:34:04 PM
bullpit I thought my code should then be the following to indicate the directory and the file name... (That was were I wanted to use d)     Am I close or am I totally off base on how to do this? 

uploads(i).SaveAs(d + c)


Never make important decisions on a Monday!
0
funluckykitty
11/20/2008 7:46:47 PM

I think you are trying to get the directory path, right. You can get it like this: 

Dim d As DirectoryInfo =  Directory.CreateDirectory("C:\UploadedFiles\" & hdnMatterID.Value & "\") 
Dim fullPath As String =  d.FullName 
Dim filePath As String =  fullPath + c
 
If you get the intellisense after you put a dot after d, you will see some properties. See if any of the properties helps you.

Thanks,
Max
Let Me Google That For You!
0
bullpit
11/20/2008 7:53:37 PM

What I'm trying to do is identify where I want files stored once a user clicks "upload".   I want them on the c drive, in the UploadedFiles folder, in the hdnMatterID sub folder (I'm trying to figure out how to add that subfolder with the name of it equal to the value in the hdnMatterID field from the form)

so my path would look like C:\UploadedFiles\123\xxxxx.doc     and then the next user who used the form would upload to  C:\UploadedFiles\124\xxxx.doc

I can concatenate the hdnMatterID onto the file name, but that isn't what I want.  I want to create a subfolder with hdnMatterID and save the files in that subfolder.  


Never make important decisions on a Monday!
0
funluckykitty
11/20/2008 7:58:35 PM

Hmmm...I noticed, in your original code, you have the ID actually enclosed within the double quotes. That is why it is not picking up the value. Try something like this.

Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\" & hdnMatterID.Value & "\")

Also, you had noticed, DirectoryInfo has a CreateSubDirectory method. If you cannot create the directory by first method here, we can try CreateSubDirectory. 

And as I mentioned earlier, you have to use DirectoryInfor.FullName to get the full path, put it in a string, and then add the file name to this path. I have showed that in my previous post.


Thanks,
Max
Let Me Google That For You!
0
bullpit
11/20/2008 8:06:14 PM
Ohh.. I'm so close... Ok, I've got it creating the subdirectory with the following code, but it won't save the file either in the subdirectory, it just seems to create the subfolder, and then nothing else happens. .  any idea why?  

If (uploads(i).ContentLength > 0) Then
Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)Dim d As New System.IO.DirectoryInfo("C:\UploadedUserFiles\")
d.CreateSubdirectory(hdnMatterID.Value)

Try
uploads(i).SaveAs("d" + c)
Span1.InnerHtml =
"File Uploaded Sucessfully."
Catch Exp As Exception
Span1.InnerHtml =
"Some Error occured."
End Try

End If


Never make important decisions on a Monday!
0
funluckykitty
11/20/2008 8:32:46 PM

This seems to work..

uploads(i).SaveAs("C:\UploadedUserFiles\" + (hdnMatterID.Value) + "\" + c)


Never make important decisions on a Monday!
0
funluckykitty
11/20/2008 9:32:53 PM

Great!


Thanks,
Max
Let Me Google That For You!
0
bullpit
11/20/2008 9:55:13 PM
Reply:

Similar Artilces:

Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'
My code is below:Protected Sub btnSendMsg_OnClick(ByVal Source As Object, ByVal E As EventArgs) Dim myMessage As New System.Net.Mail.MailMessage Dim myAttachment As System.Net.Mail.Attachment Dim myMail As System.Net.Mail.SmtpClient If Page.IsValid() Then myMessage.From = txtFromEmail.Text myMessage.To = txtToEmail.TextWhat can I do to fix this? MailMessage.To represents a collection that you Add addresses to.Dim recipient As MailAddress= new MailAddress(txtToEmail.Text) myMessage.To.Add(recipient)    Mike Banavige~~~~~~~~~~...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

Cannot convert value of parameter 'Course_FK' from 'System.String' to 'System.Guid'
Hi, I'm building a site and I've been trying to accomplish something similar to what is talked about in "Working with Data in ASP.NET 2.0 :: Declarative Parameters" tutorial. Below is the code for my methods from the xsd. I tried to attach a screenshot of my xsd but for some reason I'm not allowed GetAllCourseNames() SELECT        CourseID, NameFROM            Course GetCourseScheduleByCourseID SELECT        ScheduleID, Course_FK, Date, TeeTime, NumberOfPlayersF...

Value of type 'System.Net.Mail.MailMessage' cannot be converted to 'System.Net.Mail.AlternateView'
Trying to create an alternateview to send a txt email with the html formatted email.  I've used string builder to create the whole email, but I get this error.  Any ideas why?  i've bolded the line that throws the error... Dim EmailBodyTxt As New StringBuilder EmailBodyTxt.Append("<html><style type='text/css'>.table{border:solid; border-color:#006666; width:600;} ")EmailBodyTxt.Append("<body><table class='table'><tr><td colspan='2'><h1>Test<br />RESPONSE SYSTEM</h1></td>...

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

Value of type 'System.Data.Dataset' cannot be converted to 'String'
I receive the above error for line 57 which is: Address.text = Clientinfo(DropDownlist1.Items(DropDownList1.SelectedIndex).text) I'm trying to take the selected item from the dropdownlist1 and have the data from the query 'Clientinfo' appear using the label control called 'address'. I plan to format it to be used as a mailing address: 123 Street City, State Zip Etc. Obviously I am learning as I go. Can anyone give me an alternative or find my error. Much appreciated. The code is below: Sub Page_Load(Sender As Object, E As EventArgs) If Not Page.IsPostBa...

Value of type 'String' cannot be converted to 'System.Drawing.Color'
I have a user control for logging into a site. I place the user control on a page and in code behind set the color of the lables in the user control as such Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here UsernameLabel.ForeColor = "White" End Sub The problem I'm having is the error in the subject. I want to be able to change the font color of the label depending on the page it's placed on. Ideas? Should be: UsernameLabel.ForeColor=Syst...

System.Net.Mail is giving error CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
I'm getting an error while trying to use the wizard to create a form and than pass it the vaibles to Mail. Any help would be much appreciated. SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); try {   message.From = txtEmail.Text; message.To.Add("admin1@yoursite.com"); message.Subject = "Feedback"; message.CC.Add("admin1@yoursite.com"); message.CC.Add("admin2@yoursite.com"); message.IsBodyHtml = false; message.Body = txtMessage.Text; smtpClient.Send(message); lblStatus.Text = "Email successfully sent."; } catch (Exception ex) { lbl...

Convert 'System.DBNull' to type 'System.String' ?
I have a textbox on my site that I want to populate with some data, however when the value is null, which is quite often.The problem is that when I call a static method which returns a string; when that string is DBNull, I get the following error which is absolutely correct.Unable to cast object of type 'System.DBNull' to type 'System.String'.So, what should I do to be able to ignore that the string is null and just display a "" string in the texbox?The code is as follows:    private void GetCartSmall(string sSessionID)    {      &n...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

Object of type 'System.DBNull' cannot be converted to type 'System.String' from formview
I have a formview that I am using for input to my datatable.  I am using an objectdatasource that uses a BLL.  This in turn points to my XSD tableadapter. When I test, and only fill in the required fields, I am getting the error Object of type 'System.DBNull' cannot be converted to type 'System.String' I am not sure what field is having the issue.  In my BLL for the Add, I have checked the dates for null and placed the value of DateTime.MinValue.  For the text fields, I have place "" if they are null.  Is this error being create prior to...

Error : Value of type 'System.Data.DataTable' cannot be converted to 'String'
Hi, I have a compilation error : Value of type 'System.Data.DataTable' cannot be converted to 'String'. I want to erase all the records in my Access database and update it with a DataTable. My code lines : Dim conn As OleDbConnection Dim da As OleDbDataAdapter Dim ds As DataSet Dim sql As String sql = "SELECT * FROM menu;" conn = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=d:\www\tgc\prof_tech\fsthilaire\parametres_site\parametres_site.mdb" ) da = New OleDbDataAdapter(sql, conn) conn.Open() ds = New DataS...

BC30452: Operator '&' is not defined for types 'String' and 'System.IO.Stream'.
I try to upload an file into an image-field in my SQL but I get this error: BC30452: Operator '&' is not defined for types 'String' and 'System.IO.Stream'. What is this error and how can I solve it? Piece of the code:Dim MyFileCollection As HttpFileCollectionDim MyFile As HttpPostedFileDim FileLen, Loop1 As IntegerDim bijlage As Stringbijlage = ""Dim MyStream As System.IO.StreamDim ContentType As StringMyFileCollection = Request.FilesMyFile = MyFileCollection(0)ContentType = MyFile.ContentTypeFileLen = MyFile.ContentLengthDim Input(FileLen) As ByteMyStream = MyFile.InputStreamA...

Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'
I have a new page (wireframe) based on the User_Create page in the Time Tracker starter kit.  I have no code in the code behind page.  What might be causing this error?  This newbie needs help. I had a TextBox control named "Title" which appearantly conflicted with the page title. thanks!!! ...

Web resources about - Value of type 'System.IO.DirectoryInfo' cannot be converted to 'String'. - asp.net.getting-started

Resources last updated: 1/8/2016 7:59:23 AM