VB.NET Custom SqlFactory, FileUpload Control issue - vb.net

I've built the SqlFactory class to control my StoredProcedures, and I use it for all transactions between application and SQLServer. The part that I implemented last is file upload. But it won't do things as it should :( I've searched around the web not much could be found.
So basically I want to save my Report with name, description, and image.
Name and description are working perfectly, but for some reason it will not upload the file, it does not even reach the code for uploading it, which is:
If fuImage.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fuImage.PostedFile.FileName) OrElse fuImage.PostedFile.InputStream Is Nothing Then
'nothing to upload
Else
Dim imagebytes(fuImage.PostedFile.InputStream.Length) As Byte
fuImage.PostedFile.InputStream.Read(imagebytes, 0, imagebytes.Length)
rep.FuImage = imagebytes
End If
rep.Name = txtName.Text
rep.Description = txtReportContent.Text
repFac.Save(rep)
The lines rep.FuImage = imagebytes and those below if statement are my factory lines it should be working but it's like fuImage FileUpload control never recognizes a file that's supposed to be selected.
This is my view part:
<asp:FileUpload ID="fuImage" runat="server" CssClass="fontChange" />
<asp:RegularExpressionValidator ID="rExpImage" runat="server" ControlToValidate="fuImage"
ErrorMessage="Only .gif, .jpg, .png, .tiff and .jpeg" ValidationExpression="(.*\.([Gg][Ii][Ff])|.*\.([Jj][Pp][Gg])|.*\.([Bb][Mm][Pp])|.*\.([pP][nN][gG])|.*\.([tT][iI][iI][fF])$)"
ValidationGroup="ReportValidationSummary" CssClass="failureNotification">*</asp:RegularExpressionValidator>
Note: I am aware that FileUpload does not work in asp:UpdatePanel, hence I'm using regular asp:Panel, I beginning to doubt if there are issues with all Panel controls... Also, I've looked around problems that mention a lot of things, and I'm quite lost.
Please help, and thank you.
EDIT:
I've also tried working around forms, nothing came out of it. My server side form is placed on my Master page, and that's the only one i use. I've tried placing form on each page, instead. Nothing changed. (Read this in someones question, thought I'd try too.)

Fixed. Problem was <ContentTemplate> was surrounding both <asp:UpdatePanel> and my EditPanel which was regular <asp:Panel>. That way it won't matter that FileUpload is not within update panel. It has to be set as a full post back with <Triggers>.
Code for Triggers:
<Triggers>
<asp:PostBackTrigger ControlID="Your_Control_ID" />
</Triggers>

Related

How to change the image displayed on a button at runtime in Visual Basic?

I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).

ASP.NET Syntax to insert column in files database during upload and then to use that column to filter files into groups when downloading

I am keeping this brief so as not to make anyone assisting not have to wade through a lot of explanation and code. Please ask me for any additional explanation or code you'd like to see to help me solve this, if needed.
UPLOAD: (commented out code gets null exception. I do have DropDownList1 in the markup. I don't know what's wrong. It of course works if I hard code to "ReportGroup2" as shown.
'cmd.Parameters.Add("#Related", SqlDbType.VarChar).Value = TryCast(FindControl("DropDownList1"), DropDownList).SelectedIndex
cmd.Parameters.Add("#Related", SqlDbType.VarChar).Value = "ReportGroup2"
DOWNLOAD: (Works with hardcoded "ReportGroup2" but I don't know the syntax for the commented out line. Im trying to get the value in the Related column of the file's row)
cmd.Parameters.AddWithValue("#Id", id)
cmd.Parameters.AddWithValue("#IsItPrivate", IsItPrivate)
cmd.Parameters.AddWithValue("#Related", "ReportGroup2")
'cmd.Parameters.AddWithValue("#Related", TryCast(FindControl("DropDownList1"), DropDownList).SelectedIndex)
Again, please let me know if you need more details. My natural tendency is to explain things in great detail and include all pertinent markup code, code behind, and table structure but I have found that no one answers on any forum if I do that. The time investment is too much I think.
If you're still reading this - thanks! I will definitely come back and mark as answer who ever answers or helps. I'll also post finished code if I get it working.
I think we need a bit more information, but I'm going to take a stab in the dark.
I take it DropDownList1 is in a control like a GridView and you are trying to do something with this on a button click.
The trick to FindControl is anchoring it on the right container as it is not recursive or deep searching if you will.
YOu most likely have a row that you are working with so try something like row.FindControl("DropDownList1"), assuming you have row defined.
More info: How to get selected value from DropDownList which is inside GridView
and: http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol%28v=vs.110%29.aspx
Note:
Searches the current naming container for a server control with the
specified id parameter.
Bold added for emphasis.
In this scenario it is often common to have a variable for the control. This aids in debugging and you can have incode checks for null. Something like (I'm a C# guy so my VB is a little rusty):
Dim ddList as DropDownList
ddList = TryCast(row.FindControl("DropDownList1"),DropDownList)
If (Not ddList Is Nothing)
'Assign parameter etc
Else
'Control not found
'Do something else, perhaps throw an excpetion
End If

SetAttribute refusing to work with TextArea?

Hi, new here so I am trying to keep it specific as possible but if it's not, please just tell me nicely and I'll give more info below. :)
I'm currently making a automated message sender for the game ROBLOX, and I had it working before but they recently changed the interface and basically the entire site design / build so it broke and I'm running into a issue.
So I am familiar with SetAttribute, and that's what it used before the update. But clearly something is not working here
I currently have this:
Private Sub connect_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles connect.DocumentCompleted
Try
connect.Document.GetElementById("subject").SetAttribute("value", Form1.subject.Text)
connect.Document.GetElementById("body").SetAttribute("value", Form1.enter.Text) ' This is the line I'm having issues with
connect.Document.GetElementById("send-btn").InvokeMember("click")
Catch ex As Exception
MsgBox(ErrorToString())
End Try
End Sub
End Class
The subject line and the button are working fine, but it's not filling in the the TextArea for "enter"
This is the code for the textarea on the ROBLOX site, remember I don't work there so I cannot change it to fit my needs.
<textarea rows="2" cols="20" id="body" class="messages-reply-box text-box text" style="padding:5px;width:675px;"></textarea>
So I'm not quite sure why it's not working, I have the ID right and it works with the other textbox (the subject)
This is Visual Basic, FYI.
The "value" of a textarea is not an attribute, but the InnerText (HtmlElement.InnerText property) of the textarea-element (value is between the opening and closing tags).
Change
connect.Document.GetElementById("body").SetAttribute("value", Form1.enter.Text)
To
connect.Document.GetElementById("body").InnerText = Form1.enter.Text

Empty DataGridViews can't set a font?

I wrote a program using VB.NET. When I run the executable, it displays a DataGridView that may (or may not) have rows in it. The user can enter data, or hit an Update button to fetch data from a database. (The grid is NOT being used as a DataSource.)
The user can also set the font with another button: DataGridView1.font = "..."
If the grid DOES have rows in it, everything works as expected and the font is used in the grid.
If the grid does NOT have rows in it, the font does NOT change. Even after the user hits Update, or types in data.He has to entirely exit the program, and rerun it to see the font actually change.
Question:
How do I set the font on the grid, regardless of whether it:
Has rows.
Doesn't have rows.
Will have rows later.
I would think DataGridView1.font would ALWAYS change the font. No?
You can use the property EmptyGridview with a CssClass to change the way it displays this message.
For example:
Aspx
<asp:gridview id="EmptyGridview" runat="Server">
<EmptyDataRowStyle CssClass="gridview_vaciarow1"/>
</asp:gridview>
CSS
.gridview_vaciarow1{color:#ff0000, font-weight:bold;font-family:"Times New Roman",Georgia,Serif;}
Have a nice day!!

Vb.net click link by looking for String!

I am working on a vb.net program, I want to click a hyperlink on a page, the source look like this:
messages for Today, 2010-10-19
I want to check it everyday too!
I tried to click it with the following methods(Both couldn't click the link!):
Dim theElementCollection As HtmlElementCollection
Dim ctrlIdentity As String
theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = Today.Date.ToString(Today.Date.ToString("dd")) Then
curElement.InvokeMember("click")
End If
Next
and I tried this code too:
If Me.WebBrowser1.Document.Links(i).InnerHtml.Contains(Today.Date.ToString("dd")) Then
Me.WebBrowser1.Document.Links(i).InvokeMember("Click")
End If
Next
Any help would be appreciated! Thanks!
I've found that the best way to click links in a WebBrowser is using javascript. Try something like this:
WebBrowser1.Navigate("javascript:function%20x(){document.getElementById('foo').click()}x()")
You'll need to rewrite your above code in javascript but that's a piece of cake. You can test your javascript by copy-pasting it directly into the browser's location bar. This is also a reliable way to fill out forms.
Caveats:
Notice how the work that I want to do is wrapped in a function. This is needed if you want the javascript to do multiple statements. Wrap in a function and then invoke the function.
You can't navigate to a URL more than around 500 characters. (The limit isn't exactly 512 but it's close.) There's no warning, either, so keep it in mind.
Make sure you wait until the page is loaded. The ReadyState = Complete and IsBusy = False.
Clicking like this doesn't always generate the usual events that you get when you click a link.
"%20" is hex for space. I don't recall if this was strictly necessary in my code. Try it both ways.