VB.NET write to file - asp.net

This should be an easy one, but I'm strugling.
I've developed a web page and I'm trying to load values into a text file. I have the asp webform with a textbox and a button. When the button is pressed loads the message from the textbox to the text file.
When debugg it, it appears to work, but I can not see anything written in the file when I open it (or maybe I'm looking in the wrong place?)
When I publish it, it does not work.
Here is the code I'm using
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
' Both file_name below seem to work
' Dim FILE_NAME As String = "TB.txt"
Dim FILE_NAME As String = "..\TB.txt"
Dim line1 As String
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
'objWriter.NewLine = True
line1 = TextBox1.Text
objWriter.WriteLine(line1)
'objWriter.Write(TextBox1.Text)
objWriter.Close()
MsgBox("Text written to file")
End Sub
Any help will be much appreciated

Try this:
IO.File.WriteAllText(FILE_NAME, TextBox1.Text)
MSDN Documentation

I found that the problem was created by this line:
MsgBox("Text written to file")
Once this was removed all worked fine.
Thanks for all the advice and support

Related

value of a cell in gridview aspx page vb.net web app

I have a gridview with several columns. I need that, when the user click the button "ver" ("see" in English) I get the value of the cell(1). I inserted a textbox to see if I get the value but it doesn't return any Data. I already read a lot in the net and tried almost everything i read, and still don't know what I'm doing wrong. Anyone could help me?
The code:
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Dim codigo As String = GridView1.SelectedRow.Cells(1).Text
TextBox1.Text = codigo
End Sub
Finnaly, I got it. I deleted the button "ver" and put a ComandField ---> Select, and now it's working.

How can I save a image in a session variable and then display in asp.net and sqlserver

I' m new in this, also I'm new in Visual Basic and Sql Server, and I'm Chile...
Well, I have a question, I've been thinking for several days and can not solve.
I need to show an image in a web form of a client when he wants to see your information.
For that, I have the next sources:
Table Client
email(varchar), nameClient(varchar), nameImage(varchar)
Table Image
nameImage(varchar), image(image)
I have the next proc:
select c.email, c.nameClient, i.image
from Client as c join Image as i on c.nameImage=i.nameImage
In asp.net, I have a web Form as VerCliente.aspx, and in VerCliente.aspx.vb I have this:
Partial Class Default19
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
MostrarNombreCliente.Text = Session("correo")
End Sub
Protected Sub BotonMostrarPerfil_Click(sender As Object, e As EventArgs) Handles BotonMostrarPerfil.Click
Dim numero As Integer
Dim dvSql As Data.DataView = DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), Data.DataView)
'of this way I do the connection to BD'
If dvSql.Count > 0 Then
numero = 1
'to display the information in the textbox Iam occupying session variables'
Session("email") = dvSql(0).Item(0)
MostrarEmail.Text = Session("email")
Session("nombre") = dvSql(0).Item(1)
MostrarNombreCompleto.Text = Session("nombre")
in VerCliente.aspx I have this , and I would display the image on it
asp:Image ID="MostrarImagen" runat="server" Height="191px" Width="270px" /
but I dont know how to occupy session variables here
MostrarImagen.ImageUrl....(I dont know How )
I tried this but it does not work
Session("imagen") = dvSql(0).Item(2)
MostrarImagen.ImageUrl.ImageUrl = Session("imagen")
End If
End Sub
End Class
thank....bye!!!
Well, after of I'm trying many times, finally I can to resolve my problem, howerer I think that the way that I did the new solution is quite different to how I had posed my problem, but the important is that my solution works fine...
Here is my solution, I hope that can to be of utility for you..
Thank for your comments!!!
Session("imagen") = dvSql(0).Item(8)
If (Context.Session("imagen")) IsNot Nothing Then
Dim image As Byte() = DirectCast(Context.Session("imagen"), Byte())
Dim base64String As String = Convert.ToBase64String(image, 0, image.Length)
Image1.ImageUrl = Convert.ToString("data:image/png;base64,") & base64String
Image1.Visible = True
End If

In vb.net, one of my literal controls cannot be accessed on PreRender

I'm completely stumped by this problem.
I have:
Protected Overrides Sub CreateChildControls()
Dim lcStartButtonText As New LiteralControl
lcStartButtonText.ID = "lcStartButtonText"
Dim lcCancelButtonText as New LiteralControl
lcCancelButtonText.ID = "lcCancelButtonText"
With Controls
.Add(lcStartButtonText)
.Add(lcCancelButtonText)
End With
MyBase.CreateChildControls()
End Sub
Then, so that I can access token replacement I'm populating the literal controls OnPreRender... Removing tokens for simplicity and manually adding text, it still does not work. Example:
Protected Overrides Sub OnPreRender(e As EventArgs)
MyBase.OnPreRender(e)
Dim lcStartButtonText As LiteralControl = FindControl("lcStartButtonText")
lcStartButtonText.Text = "<span>Start</span>"
Dim lcCancelButtonText As LiteralControl = FindControl("lcCancelButtonText")
lcCancelButtonText.Text = "<span>Cancel</span>"
End Sub
What's happening is when the control is rendered the cancel is empty.
The above is a simple example. My code is long, but follows the same pattern for each literal control being rendered. I've double, triple, quadruple checked and copied and pasted to ensure no typos.
Because my example is simple, I'm mainly looking for advice as to what might be causing this based on someone else's experience who may have run into this problem.
I have like 30 literal controls and every one is working except the cancel one.
Any thoughts?
Thank you!

Make asp.net webpage respond to WebClient request

I have run into a problem in my asp.net and vb.net programming. I have made a website which connects to a database to authenticate users. I have made another page which my windows form application uses to authenticate users so the program does not have to connect directly to the MySql Database as this is insecure.
So far, the code for my asp.net page is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString.Keys(0) = "key" And Request.QueryString.Keys(1) = "getapikeyv1" And Request.QueryString.Keys(2) = "authapienc2" Then
Dim key As String = Request.QueryString.Get("key")
If key = "dua9r3rqpK{WPDWp9r93af03r8yqrEPIF{A}W~DW)D08q3raewdawdug9q8wdyaw9wq" Then
returnLabel.Text = "XYeJay4XVh6amvf1kJ34Y6y6hR0sSokP9oJFsi0Mxl4QB0T86W0iac9o2nRfCmFmsONq6sPz+rdX+/NqFk3inHRGnwnqf0IsQXE/M/SvnvY="
Else
returnLabel.Text = "invk"
End If
Else
returnLabel.Text = "invalidrequest"
End If
End If
End Sub
The default value for 'returnLabel' is INVALIDREQUEST. My api keys defined above are working fine so are not needed to be considered here. However, when I use the following code to communicate with the webpage, it always returns with returnLabel's value as 'INVALIDREQUEST'. Even if I put the returnLabel.Text = "1233" at the beginning of the Me.Load sub, the page still returns INVALIDREQUEST when I communicate with it through my program!
I use this code to request:
Try
Dim APIRequest As New WebClient
APIRequest.QueryString.Add("key", r9j0wqfha0fh0wf0.DecryptString128Bit(APIKey5, dVadaihwdq93ra0w0))
APIRequest.QueryString.Add("getapikeyv1", "")
APIRequest.QueryString.Add("authapienc2", "")
Dim ako39rqwfa As String = APIRequest.DownloadString(EncryptionPageUrl)
MsgBox(ako39rqwfa)
Dim m As Match = Regex.Match(ako39rqwfa, "<span id=""returnLabel"">.*?</span>")
APIKey00 = m.Value.Replace("<span id=""returnLabel"">", "").Replace("</span>", "")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
As you can see, the WebClient I use is fine but the Me.Load sub in the webpage is not responding the request made by the program...
Is there any way I can make the webpage respond to the WebClient.DownloadString() function?
Thanks,
Rodit
(ps. c# or vb code would be appreciated)
Change your asp.net page to check the query string like this, not sure if you can assume they are in a certain order:
If Request.QueryString.Get("key") isnot nothing And
Request.QueryString.Get("getapikeyv1") isnot nothing And
Request.QueryString.Get("authapienc2") isnot nothing Then

Using and accessing Webresource

I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.

Resources