My Scenario,
I reloaded ny page with previous data that was available for my login.Now i try to edit the textbox and update .It either updated or doesnt retain the edited value in Textbox.
objConn.Open()
Dim myControl As TextBox = FindControl("txtName")
Dim cmd As New SqlCommand("sp_UpdateNewmember", objConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#UserName", SqlDbType.VarChar).Value = Username
cmd.Parameters.Add(New SqlParameter("#FirstName", DirectCast(myControl, TextBox).Text))
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = txtlastname.Text
Anyone can help please.
When a page is requested very first time, you have to fetch a row from the table and assign values to the controls properties.
protect Sub Page_Load()
IF Not IsPostBack Then
//Retrieve a record
End If
End sum
To update a record, code should be like this:
Dim myControl As TextBox = FindControl("txtName")
Dim cmd As New SqlCommand("sp_UpdateNewmember", objConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#UserName", SqlDbType.VarChar,30).Value = Username
cmd.Parameters.Add("#FirstName",SqlDbType.VarChar,30).Value= myControl.Text
cmd.Parameters.Add("#LastName", SqlDbType.VarChar,30).Value = txtlastname.Text
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close()
please write code inside this
if (!ispostback)
{
//code
}
Related
One of our ASP web apps has a button, that generates a expiring link to a form. We have noticed that anyone with said link can view the form. I am researching a way to have the end-user be checked for authorization when the link is opened up. Ask them for their username/password before they can view the link.
my sub that generates the link:
Protected Sub ButtonLink_Click(sender As Object, e As EventArgs)
Dim strSQL As String = ""
Dim wapguid As String = ""
Dim cmd As New SqlCommand
wapguid = System.Guid.NewGuid().ToString("N")
strSQL = "INSERT INTO [WapCustomerAccess] (wapid, wapguid, expires, generatedBy) values (#wapid, #wapguid, #expires, #generatedBy)"
cmd.CommandType = CommandType.Text
cmd.CommandText = strSQL
cmd.Parameters.AddWithValue("#wapid", WAPID)
cmd.Parameters.AddWithValue("#1", wapguid)
cmd.Parameters.AddWithValue("#generatedBy", Session.Item("UserFullName"))
cmd.Parameters.AddWithValue("#expires", Date.Now.AddDays(31).ToString)
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("cnnCFHSWAP").ConnectionString
Dim con As New SqlConnection(strConnString)
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message & "<br />")
Finally
con.Close()
con.Dispose()
End Try
Dim t As New TextBox()
t.Text = "http://wap-test.cfhs.local/secure/waplink.aspx?SSL=true&hash=" & WAPID & "&guid=" & wapguid & "&expires=45&suid=05A2FF&salt=x00FF&enc=true"
t.ID = "txtLink"
t.Width = 800
t.ReadOnly = True
t.BackColor = Drawing.Color.SeaShell
PanelLink.Visible = True
PanelLink.Controls.AddAt(0, t)
PanelLink.Controls.Add(New LiteralControl("<br />"))
End Sub
You will need to restrict access to waplink.aspx in the app that is referred to in your generated links. Depending on that app, you will find suitable tutorials for setting up authentication/authorization, like [1].
[1] https://weblogs.asp.net/gurusarkar/setting-authorization-rules-for-a-particular-page-or-folder-in-web-config
I got 1 checkboxlist with 6 checkboxes inside it (below is my database)
id : int
interest : bit
When I click one checkbox, the value is saved to my database as TRUE. Here is my code:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim insertSql As String = "INSERT INTO tbinterest(interest) VALUES(#interest)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("#interest", SqlDbType.Bit).Value = 1
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
But somehow, when I load the page, it does not show the checked state (checked/unchecked). Here is my code in page load. Can you help me out? Thanks.
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Using myConnection As New SqlConnection(connectionString)
Dim objCmd_team As SqlCommand = New SqlCommand("SELECT [interest] FROM [tbinterest]", myConnection)
myConnection.Open()
Dim objReader As SqlDataReader = objCmd_team.ExecuteReader()
While (objReader.Read())
Dim currentCheckBox As ListItem = chkApprovers.Items.FindByText(objReader("interest"))
If currentCheckBox IsNot Nothing Then
currentCheckBox.Selected = True
End If
End While
End Using
your code seems to be correct.. please check this line with break point that listitem has value or not.
Dim
currentCheckBox As
ListItem =
chkApprovers.Items.
FindByText(objReader
("interest"))
If listitem is nothing then try this
Dim
currentCheckBox As
ListItem =
chkApprovers.Items.
FindByText(objReader.Item
("interest").ToString)
And one more thing, you are saving only bit value as interest then how did you match this with your checkbox.
you can match only with checkboy text value or id value
Hope this help.
When I use the code below for some reason when Update is clicked, hospitaltextbox.text does not show the current value in the textbox? Just the value that was originally selected. Any idea why it is not reading what is currently in the textbox?
Dim reader As SqlDataReader = cmd.ExecuteReader()
If (reader.Read()) Then
HospitalTextBox.Text = reader(7)
FirstNameTextBox.Text = reader(9)
Session("ID") = reader(0)
End If
Protected Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
Dim Test As String
Test = FirstNameTextBox.Text
Try
Dim Con As SqlConnection
Dim cmd As SqlCommand
Con = New SqlConnection
Con.ConnectionString = ""
Con.Open()
cmd = New SqlCommand
cmd.Connection = Con
cmd.CommandText = "UPDATE tbltest SET [Teaching Hospital Name] = #TeachingHospitalName, [Physician First Name] = #FirstName WHERE ID = #ID"
cmd.Parameters.Add(New SqlParameter("#ID", (Session("ID"))))
cmd.Parameters.Add(New SqlParameter("#TeachingHospitalName", HospitalTextBox.Text)) 'does not show text that was changed in the textbox?
cmd.Parameters.Add(New SqlParameter("#FirstName", Test))
cmd.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
End Try
End Sub
Check your Page_Load function.
Are you doing something on PostBack to set the textbox value to something else?
If you're binding data, make sure it's done when Not IsPostback
If Not IsPostBack
BindData()
Hi I'm about to update some data through a textbox created on a dynamic filled Gridview but sadly when I click the update button, my code cannot read what I have inputed on those textboxes. Heres my code:
sqlcon = New SqlConnection(conString)
Dim id = GridAnswers.DataKeys(e.RowIndex).Value
Dim row As GridViewRow = TryCast(GridAnswers.Rows(e.RowIndex), GridViewRow)
Dim ChoiceDescription As TextBox = TryCast(row.FindControl("ChoiceDescription"), TextBox)
Dim Rating As TextBox = TryCast(row.FindControl("Rating"), TextBox)
DropDownList)
Using sqlcon As New SqlConnection
Dim sql As String = "UPDATE CS_RefQuestionChoice SET ChoiceDescription=#choice, RatingID=#ratingid WHERE ChoiceID=#qid"
Using cmd As New SqlCommand(sql, sqlcon)
cmd.Parameters.AddWithValue("#choice", ChoiceDescription.Text)
cmd.Parameters.AddWithValue("#ratingid", Rating.Text)
cmd.Parameters.AddWithValue("#id", id)
sqlcon.Open()
cmd.ExecuteNonQuery()
sqlcon.Close()
End Using
End Using
GridAnswers.EditIndex = -1
gridbind()
Check to bind the gridview only if the page is not posted back.
I have the following code on my default.aspx
Label1.Text = Session("valueName").ToString()
And the following code on my login.aspx
Dim strCon As String = ConfigurationManager.ConnectionStrings("Goed").ConnectionString
'Create Connection String And SQL Statement
Dim strSelect As String = "SELECT COUNT(*) FROM tbl_LogIn WHERE Gebruiker = #Gebruiker AND Wachtwoord = #Wachtwoord"
Dim con As New SqlConnection(strCon)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = strSelect
Dim Gebruiker As New SqlParameter("#Gebruiker", _
SqlDbType.VarChar)
Gebruiker.Value = TxtUs.Text.Trim().ToString()
cmd.Parameters.Add(Gebruiker)
Dim Wachtwoord As New SqlParameter("#Wachtwoord", _
SqlDbType.VarChar)
Wachtwoord.Value = TxtPw.Text.Trim().ToString()
cmd.Parameters.Add(Wachtwoord)
con.Open()
Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32)
con.Close()
If result >= 1 Then
Response.Redirect("default.aspx")
Session("valueName") = TxtUs.Text.ToString()
Else
lblMsg.Text = "Gebruikers naam en of wachtwoord kloppen niet"
End If
End Sub
But it doesnt seem to help. I don't get any error or whatso ever, any idea?
The Redirect method ends the execution, so you have to set the session variable before redirecting:
Session("valueName") = TxtUs.Text.ToString()
Response.Redirect("default.aspx")
you are redirecting before setting the session. you should first set the session and then redirect the page.
Or
Your result variable is not 1 or bigger than 1. you should check that too