DetailsView data to Textbox - asp.net

After long time looking for a solution I found one here - I thought....
The code I tried to implement was:
If DetailsView1.Rows.Count > 0 Then
Dim row As DetailsViewRow
For Each row In DetailsView1.Rows
TextBox3.Text &= row.Cells(0).Text & " = " & row.Cells(1).Text & " "
Next
Else
TextBox3.Text = "No data found"
End If
As result I only got the line label. The row.Cells(1) did not return any data.
My DetailView show correct data from my database (total 7 lines). I need to merge these data together with some other user input fields and create an output file.
My questions are:
1) How do I get the detailview data into a textbox
2) My final task is to create an output.txt file
I have to use VB since I'm unfamilar with C#.

Related

microsoft access 2010 Not In List event

I am VERY new to access and am working on a database. I have a form (frmHomeowner) with a bound multi-column combo box for zip codes (cboZip). The combo box is based on a query from tblZipCity [zipid (PK), zipcode, city, state] and the city and state text boxes get filled automatically once a zip code is selected.
I am trying to add the option of adding a new zipcode to the list with the Not In List event of the cboZip by opening the frmCitiesZip to add the new record :
Private Sub cboZip_NotInList(NewData As String, Response As Integer)
Dim Result
Dim Msg As String
Dim CR As String
CR = Chr$(13)
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Ask the user if he or she wishes to add the new zip code.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
' If the user chose Yes, start the CityZip form in data entry
' mode as a dialog form, passing the new zip code in
' NewData to the OpenForm method's OpenArgs argument. The
' OpenArgs argument is used in the homeowner form's Form_Load event
' procedure.
DoCmd.OpenForm "frmCitiesZip", , , , acAdd, acDialog, NewData
End If
' Look for the zip code the user created in the zip code form.
Result = DLookup("[zipcode]", "frmCitiesZip", _
"[zipcode]='" & NewData & "'")
If IsNull(Result) Then
' If the zip code was not created, set the Response argument
' to suppress an error message and undo changes.
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
' If form's OpenArgs property has a value, assign the contents
' of OpenArgs to the zipcode field. OpenArgs will contain
' a zip code if this form is opened using the OpenForm
' method with an OpenArgs argument, as done in the homeowner
' form's cboZip_NotInList event procedure.
Me![zipcode] = Me.OpenArgs
End If
End Sub
But I seem to run into a problem around this line:
Result = DLookup("[zipcode]", "frmCitiesZip", _
"[zipcode]='" & NewData & "'")
If IsNull(Result) Then
When I type a zip code not currently on the list I get the prompt asking me if I want to add to the list but after I add it to the frmCitiesZip and close the form I get an error message:
Run-time error ‘3078’ saying that access cant find the input table or qry ‘frmCitiesZip’.
The frmCitiesZip is based on qryCitiesZip. Not sure what I am doing wrong. Any help would be appreciated. Thanks!

Separating values in a textbox by special character

I have an asp.net/vb.net web app that requires information to be put into a multiline text box. If the user hits enter while in the textbox it drops down to next line, and they can enter more data in. When it tries to go to the database it fails because of the way the data is represented. I need help with looping through the field and getting each value.
This is how the data is represented in the DataTable
0;0.123;0.234;0.345;...
I need each value between the ; character... so I was thinking something like this?
If dbRow.Item("PV").ToString.Contains(";") Then
For Each symbol As String In DBTable.Rows
'get data b/w the ';'
Next
End If
Any help would be greatly appreciated
Edit:
If dbRow.Item("PV").ToString.Contains(";") Then
For Each s As String In dbRow.Item("PV").ToString
Dim fields() As String = s.Split(";"c)
For Each value As String In fields
.Append("'" & CDbl(value) & "'," & "SysDate,1)")
DBCommand.CommandText = myInsertIntoProperty_DataStringBuilder.ToString
DBCommand.ExecuteNonQuery()
myInsertIntoPropertyStringBuilder = New StringBuilder
Next
Next
Else
.Append("'" & CDbl(dbRow.Item("PV")) & "'," & "SysDate,1)")
End If
You mean something like this?
Dim s As String In dbRow.Item("PV").ToString()
Dim fields() As String = s.Split(";"c)
For Each value As String In fields
' Do what you want with the value
Next
Then access each value with field(0), fields(1), etc. You can then convert it to the appropriate type, for example:
Dim value As Double = Double.Parse(fields(0), CultureInfo.InvariantCulture)

CDO mail - adding a dynamic list to myMail.HTMLBody

I have a CDO mail notification that I wish to list various records in the HTMLBody so the recipient can see what's been updated in my db. I can produce the list and display it if I use Response.Write so I know my record set contains more than a single record - but when I add this to HTMLBody it will only display the last record and not the list.
Any ideas why this isn't working and only putting a single record into HTMLBody?
Set myMail=CreateObject("CDO.Message")
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "XXXXXXXXXX"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = ""
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ""
myMail.Configuration.Fields.Update
myMail.Subject= "Test 4"
myMail.To= mailto
myMail.From="XXXXXXXXX"
myMail.HTMLBody="Date Insert From Super Calendar<br/>"
if Not rsSQL_cal_adIdlist.EOF then
Do While Not rsSQL_cal_adIdlist.EOF
myMail.HTMLBody = "Id Number:"&rsSQL_cal_adIdlist.Fields("adId")&"<br />"
rsSQL_cal_adIdlist.MoveNext
Loop
end if
myMail.Send
set myMail=nothing
It changes the value of mymail.htmlbody several times before it sends, it doesn't add to it.
You need to create a variable, and concatenate your DB rows to it, something like
Dim mymessage
mymessage = "Date Insert From Super Calendar<br/>"
if Not rsSQL_cal_adIdlist.EOF then
Do While Not rsSQL_cal_adIdlist.EOF
mymessage = mymessage & "Id Number:"&rsSQL_cal_adIdlist.Fields("adId")&"<br />"
rsSQL_cal_adIdlist.MoveNext
Loop
end if
Then you can create your CDO.Message object later on in the script and use
myMail.HTMLBody = mymessage

Filter taking 2 seconds on small adbodb recordset

I have a small adodb recordset I am trying to filter. This one is 6 records for our test customer.
For some reason the filter is taking 2 seconds to complete, and I am doing this around 30 times on my asp page. Thus, making my page really slow to load. The other recordset filters on this page are running fast.
I have tried setting different CursorLocations and CursorTypes..
Can anyone help me determine why this filter is so slow?
rsAvgPrice.Filter = "CommodityID = 13 AND CropYear = '12'"
Probably the whole query is executed again and only then the filter is being applied.
I would have one single loop over all the items, store the required data in local variables then have my own filter. Best efficiency, much better control.
For example, if you want the data filtered by those two fields, I would use Dictionary like this:
Dim oCommodity_CropYear_Data, curKey
Dim curCommodityID, curCropYear, curData
Set oCommodity_CropYear_Data = Server.CreateObject("Scripting.Dictionary")
Do Until rsAvgPrice.EOF
curCommodityID = rsAvgPrice("CommodityID")
curCropYear = rsAvgPrice("CropYear")
curKey = curCommodityID & "_" & curCropYear
curData = "field1: " & rsAvgPrice("somefield") & ", field 2: " & rsAvgPrice("other_field") & "<br />"
oCommodity_CropYear_Data(curKey) = oCommodity_CropYear_Data(curKey) & curData
rsAvgPrice.MoveNext
Loop
rsAvgPrice.Close
Then to extract the data in a loop:
For x=1 To 30
For y=1 To 12
curKey = x & "_" y
If oCommodity_CropYear_Data.Exists(curKey) Then
Response.Write("Data for Commodity " & x & " and CropYear " & y & ":<br />" & oCommodity_CropYear_Data(curKey)
End If
Next
Next
This is the general idea, hope you can use it for your actual needs.
I have resolved this issue.
The issue was when I declare a record set the following way, the cursor type gets set as adOpenForwardOnly and the cursor location to adUseServer. These settings cannot be changed if you fill your recordset using command.Execute.
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandType = adCmdText
cmd.CommandText = mySQL
cmd.CommandTimeout = 3000
cmd.ActiveConnection = cn
Set rs = Server.CreateObject("ADODB.Recordset")
Set rs = cmd.Execute
Set cmd = Nothing
The way I resolved this was manually declaring a permanent recordset with its fields. Then I filled a temporary recordset using the command.execute. I then manually populated my declared recordset with the temporary recordset record by record. This allowed me to set the cursorlocation to adUseClient.
Thus speeding up the filter by leaps and bounds.

How do I get all the values in a listbox when looping through them?

I'm currently trying to move through all the values added to a listbox by the user, however, I want to retrieve the actual value of each item in the listbox and not the text.
I've gotten so far with the code below, but that only gets the text and not the value.
For Each item In SelectedStoresLB.Items
Dim tCompany As Integer = CInt(Left(item.ToString, 1))
Dim tStore As String = Right(item.ToString, 3)
Dim tReason As String = ReasonTxt.Text
insertSQL = "INSERT INTO [CommsDownLog] ([DimCompanyID],[PervasiveStoreNumber],[DownReason]) VALUES (" & tCompany & ", '" & tStore & "', '" & tReason & "')"
Dim insertRow = New SqlCommand(insertSQL, objConn)
Try
objConn.Open()
insertRow.ExecuteNonQuery()
objConn.Close()
Catch ex As Exception
Response.Write(ex)
End Try
Next
How would I go about getting the value for each item in the collection?
item is a ListItem object - rather than call ToString on it, you should use the Text and Value properties to get the info you need.
Using VB 2010, note to get the actual values of the items in the listbox you need to use the "Content" property of the ListBoxItem object. Eg:
For i As Integer = 0 To lstSortUs.Items.Count - 1
sAllItems &= lstSortUs.Items(i).Content & ";"
Next
sAllItems = Left(sAllItems, Len(sAllItems) - 1)
arrAllItems = sAllItems.Split(";")
System.Array.Sort(arrAllItems)
Have you tried:
item.Value
You need to be careful when iterating over a ListBox because you may end up modifying the underlying collection. By using foreach as you are, you are utilizing the underlying enumerator. I recommend you modify your iterator to the following (C# example):
foreach (ListItem li in listbox.Items.ToArray())
{
if (li.Selected)
{
Controltest2.Remove(li.Value);
}
}
By doing this, you are modify the Array's collection and not the list's collection. This assumes LINQ to object and you may need to call Cast<t> to make it work in some cases.
The reason for this is below:
The foreach statement repeats a group
of embedded statements for each
element in an array or an object
collection. The foreach statement is
used to iterate through the collection
to get the desired information, but
should not be used to change the
contents of the collection to avoid
unpredictable side effects
Source: MSDN
To get the text you want after iterating, use .Value instead of .Text. Of course, there are other ways to iterate such as going in reverse with an indexed for loop, but that's another topic :)

Resources