a method to prevent duplicate entry in multiline textbox - asp.net

So I try to write a a code to prevent duplicated entry from register at all. So I've tried this but it doesn't work like how I want it to.
Private Sub NoDuplicate1()
For Each line As String In Me.TxtResult4.Text.Split(vbLf)
If line = TxtResult4.Text Then
LblMsg.Text = ""
TxtResult4.Text = TxtResult4.Text.Remove(TxtResult4.Text.LastIndexOf(Environment.NewLine))
End If
Next
End Sub
I've also try this code and put in postback and it work:
If Not IsPostBack Then
TxtResult3.Text = String.Join(Environment.NewLine,
TxtResult3.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
TxtResult4.Text = String.Join(Environment.NewLine,
TxtResult4.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
End If
but the problem that I have with the code is that the duplicate data only deleted when the page refresh. when what I want to do is to block/prevent the duplicate data from being enter at all . Is there any suggestion on how I can modified my code ?

This should remove any duplicate lines in the TextBox.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim splitter() As String = {Environment.NewLine}
Dim lines() As String = TextBox1.Text.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
Dim dupFree = lines.Distinct.ToArray
TextBox1.Text = Join(dupFree, Environment.NewLine)
End Sub

Related

Open picturefiles with Openfiledialog and list them in CheckedListBox without filepath and show fullpath in Textbox for each item when selected VB

I am using Visual Basic code:
Openfiledialog > I want to keep using this.
CheckedListBox
Textbox
Picturebox
My code below is working but I want the CheckedListBox to show only the filename(safefilename) for each image.jpg item from folder and show the fullpath with filename for each CheckedListBox.item selected to show in my textbox, I don't know and can't find the right code for this.
(now I am using the fullpath aka openfiledialog1.filenames) somehow I need to combine the openfiledialog.safefilenames with openfiledialog.filenames..
my code sofar:
Private Sub Button1_Click(sender As Object, e As
EventArgs) Handles Button1_Click
openFiledialog1.Filter = "Pictures(*.jpg*)|*.jpg"
If openFiledialog1.ShowDialog =
Windows.Forms.DialogResult.OK Then
For Each f As String In openFiledialog1.FileNames
SuspendLayout()
CheckedListBox1.Items.Add(f.ToString)
ResumeLayout()
TextBox1.Text = f
For i As Integer = 0 To
CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i,True)
Next
end if
Private Sub
CheckedListBox1_SelectedIndexChanged(sender As
Object, e As EventArgs) Handles
CheckedListBox1.SelectedIndexChanged
TextBox1.Text = CheckedListBox1.SelectedItem
If Not CheckedListBox1.SelectedIndex < 0 Then
Try
Dim img As Image =
Image.FromFile(CheckedListBox1.
SelectedItem.ToString())
PictureBox1.BackgroundImage = img
Catch ex As Exception
End Try
End If

Can't update data when value is assigned to textboxes in load page

My problem is probably stupid due to my inexperience to programming but please help.
So, I set some textboxes to values from my other page so the user can edit their Task and see what their task was original and edit from there.
The problem is that I set those values in Page Load so when I tried to update it, it updated the original value not the new one that entered because the page load return the old value. Because of this I cannot update my database to new value.
Is there anyway to approaches this problem or to fix this? Like should I place the code somewhere else?
Edit: Ok so I did some more research and found that my problem could maybe be solved by using IsPostBack but I don't understand how to use this code. The only thing I kinda understand is that it is used to return the page to before it got refresh by page load?
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("Editing") IsNot Nothing Then
btnCreate.Visible = False
btnEdit.Visible = True
Dim Form As FormViewRow = CType(Session("Editing"), FormViewRow) 'get data from the the formview in Home page
Dim Title As Label = CType(Form.FindControl("TitleLabel"), Label)
txtTitle.Text = Title.Text
Dim description As Label = CType(Form.FindControl("DescriptionLabel"), Label)
txtDescription.Text = description.Text
Dim diff As Label = CType(Form.FindControl("DifficultyLabel"), Label)
ddlDifficulty.Text = diff.Text
Dim taskID As Label = CType(Form.FindControl("IDLabel"), Label)
Dim whatID As String = taskID.Text
Session("EditTaskID") = taskID.Text
End If
End Sub
Ok so I was just playing around with IsPostBack and managed to get it to work.
All I did was put my code in an If statement IsPostBack is not equal True Then... Since I'm still learning can someone explain the meaning of IsPostBack in the simplest terms?
It's fine if no one explain it to me. My problem is solved and thank you Amin for taking your time trying to help me. (:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If IsPostBack <> True Then
If Session("Editing") IsNot Nothing Then
btnCreate.Visible = False
btnEdit.Visible = True
Dim Form As FormViewRow = CType(Session("Editing"), FormViewRow) 'get data from the the formview in Home page
Dim Title As Label = CType(Form.FindControl("TitleLabel"), Label)
txtTitle.Text = Title.Text
Dim description As Label = CType(Form.FindControl("DescriptionLabel"), Label)
txtDescription.Text = description.Text
Dim diff As Label = CType(Form.FindControl("DifficultyLabel"), Label)
ddlDifficulty.Text = diff.Text
Dim taskID As Label = CType(Form.FindControl("IDLabel"), Label)
Dim whatID As String = taskID.Text
Session("EditTaskID") = taskID.Text
End If
End If
End Sub
I don't know whether your editing page is opened in the same window or a new one. In first scenario, you can save edited values somewhere like Session and restore them in main page's Page_Load server event. According to 2nd scenario, you can use some simple javascript codes to reflex data changes to main page.

ASP.NET Control added to Placeholder lost values right after adding

Working over 5 hours on the following problem:
Private Sub ModulEdit_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim modulid As Integer = 1
loadeditors(modulid)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub loadeditors(modulID As Integer)
PlaceHolder1.Controls.Clear()
Using dbContext As New EntitiesModel()
Dim mps As List(Of ef.Modulparameter) = dbContext.Modulparameters.Where(Function(c) c.ModulID = modulID).ToList
Dim mmid As Int16
If EditMode.Checked = True Then
mmid = RadComboBox3.SelectedValue
End If
Dim mp As ef.Modulparameter
For Each mp In mps
Dim lbl As New Label
lbl.Text = "<BR>" & mp.Name & "<BR>"
PlaceHolder1.Controls.Add(lbl)
Select Case mp.Editor.Name
Case "textbox1line"
Dim con As New TextBox
con.ID = mp.ID
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Text = mpa.Valuestring
Catch ex As Exception
con.Text = "AAAA"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
Case "radeditor"
Dim con As New RadEditor
con.ID = mp.ID
con.ToolsFile = "\admin\controls\ToolsFile.xml"
'con.CssFiles.Add("\Content\frenzy\css\frenzy-orange.css")
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Content = mpa.Valuestring
Catch ex As Exception
con.Content = "BBBB"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
End Select
Next
End Using
End Sub
I add the control dynamicly, calling the codepart above in pre_init (tryed in load and init too with same result)
The value (text) for the control is there until that line PlaceHolder1.Controls.Add(con)
After the con.text is empty.
The control is added after, but with no value.
Strange, that in the same proc i add another control (label), where the text value is on the page after.
Adding additional info:
the control value (text or content), when debugging the LoadEditors), is allways correctly set. But then on the page both (textbox and radeditor) are empty
The routing is called from pre init, as described in a lot of related posts.
You are calling loadeditors in ModulEdit_Init. Shouldn't this be LoadControls ?
I fixed it myself:
Adding "con.ViewStateMode = System.Web.UI.ViewStateMode.Disabled" before adding control to placeholder
Calling "loadeditors()" in RadComboBox3 too
much probably the problem was, that i loaded editors in page-load or init, which got the correct values, but then the RadComboBox3.SelectedIndexChanged event was called, which overwrote the values somehow
So my answer is not a real answer, but it works now (I hate such: it works, but i dont know why) ;)

How to match the answer in vb.net and asp.net?

I random the 3 question to display on asp.net page. I want to match the security question with correct answer in database. But after I enter the correct answer in textbox and click the "Next" button, the answer cannot match the current question displayed on screen. But if I click the "Next" button, the next question displayed is matched with answer I enter on textbox in previous question. I think this is because the answer is match the question and answer after refresh the page. Please help. Here is my code. thanks
vb.net code
Dim SecurityQuestion As New DBDataContext
Dim randomNumber As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SecurityQuestion As New DBDataContext
Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
ValidateAnswer()
End Sub
Private Sub LoadSecurityQuestion()
Dim SecurityQuestion As New DBDataContext
Dim mySecurityQuestion = (From c In SecurityQuestion.tblSecurityQuestions Where c.PkID = randomNumber Select c)
Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
randomNumber = New Random().Next(1, rowCount)
With gvQuestion
.DataSource = mySecurityQuestion
.DataBind()
End With
End Sub
Private Sub ValidateAnswer()
Dim SecurityQuestion As New DBDataContext
Dim validate = (From r In SecurityQuestion.tblRegistrations From s In SecurityQuestion.tblSecurityQuestions _
Where s.PkID = r.Q01 And r.A01 = txtSecurityAns.Text And r.UserID = ad And s.PkID = randomNumber _
Select r)
If validate.Count > 0 Then
Msg3.Text = "Correct Answer"
Else
Msg3.Text = "Invalid Answer"
End If
End Sub
At the time I posted this answer, it looked like a small bit of your code might have been missing. As an example, I couldn't see where you were actually calling the method LoadSecurityQuestion, but it sounds like you may be acidentally re-calling the method LoadSecurityQuestion even after validation succeeds.
I think you can fix this problem by wrapping your call to LoadSecurityQuestion with an "if" qualifier. As an example, lets suppose that you were calling LoadSecurityQuestion in your Load Event handler. If you did that, your new Load event handler might read something like this:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
If IsPostBack Then
' If you wrap all of your Me.LoadSecurityQuestion calls
' with "If" statements like this one, then it should keep
' the security question from changing when the user
' enters the correct answer
If Msg3.Text <> "Correct Answer" Then
Me.LoadSecurityQuestion()
End If
End If
...
End Sub

How to sort a gridview once a radio button is selected

I'm trying to sort records in the gridview right after a radio button is selected. My approach is with the dataview, but because the dataset variable doesn't survive a round trip to the server, I don't know how to make this happen. please help!
Public Sub GetCustomers()
db.RunProcedure("usp_customers_get_all")
db.doSort(radList.SelectedValue)
gvCustomers.DataSource = db.MyView
End Sub
Protected Sub radList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radList.SelectedIndexChanged
If radList.SelectedValue = 0 Then
db.doSort(0)
gvCustomers.DataSource = db.MyView
End If
If radList.SelectedValue = 1 Then
db.doSort(1)
gvCustomers.DataSource = db.MyView
End If
End Sub
Public Sub doSort(ByVal strIn As Integer)
If strIn = 0 Then
MyView.Sort = "lastname, firstname"
Else
MyView.Sort = "username"
End If
End Sub
Public Sub RunProcedure(ByVal strName As String)
Dim objConnection As New SqlConnection(mstrConnection)
Dim mdbDataAdapter As New SqlDataAdapter(strName, objConnection)
Try
mdbDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
Me.mDataset.Clear()
mdbDataAdapter.Fill(mDataset, "tblCustomers")
MyView.Table = mDataset.Tables("tblCustomers")
Catch ex As Exception
Throw New Exception("stored procedure is " & strName.ToString & " error is " & ex.Message)
End Try
End Sub
You could store the dataset in one of the following places and then when the post back happens just load it again from there. I have done many of these on a corporate intranet.
Session Variable
ViewState
QueryString
Cache
I cant really provide more help as you didn't specify if this is done in Ajax or if you do a full postback etc. If you provide more info I would love to help you.

Resources