Display newly created record in Formview - insert

I have this form on VS2012 with asp.net. First I do search for the patron, then go to verify information for that patron. This patron information is displayed in ItemTemplate(ReadOnly). If that is not the patron they are looking for then they can add a new patron with "New button" (asp.net code). I am able to get the id of the new Patron(which is PK). However I am not able to display this newly created record on the form after inserting. It still displays the record which was on display. Since this a formview I did not enable "Paging".
Is it possible to call the pageload event from datasource_inserted event? Then I can pass the new patron ID for display. I declared this ID as global variable?
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim lvPatronID As String
lvPatronID = Request.QueryString("value1")
If lvPatronID = "" Then
frmPatronView.ChangeMode(FormViewMode.Insert)
Else
frmPatronView.ChangeMode(FormViewMode.ReadOnly)
GvPatronID = lvPatronID
lblPatronID.Text = GvPatronID
End If
Protected Sub PatronDS_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles PatronDS.Inserted
NewID = e.Command.Parameters("#PatronID").Value.ToString
GvPatronID = NewID
End Sub

Well I answered part of my own question. The following change to the Inserted event will let me view the newly inserted data. I have another button to add new record in the emptytemplate of the search form. This is why I am changing the mode to insert as the default mode is readonly. This will let me insert the data but after inserting it doesn't display the form at all. Not sure why Inserted event is not kicking in properly.
Protected Sub PatronDS_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles PatronDS.Inserted
Dim NewID As String = Nothing
Try
NewID = e.Command.Parameters("#PatronID").Value.ToString
PatronDS.SelectCommand = "SELECT * FROM tblPatron WHERE PatronID='" & NewID & "'"
lblPatronID.Text = NewID.Trim()
frmPatronView.DataBind()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Related

LINQ to SQL, Visual Basic - Listbox Receives the Query NOT the Result

Overview
This is a homework assignment using LINQ to SQL in a Visual Basic application. It is correct in most ways except that I have a partially broken result. Rather than adding the result of my second query to my listbox, my code adds a weird representation of the query itself. Below is a description of the DB, followed by my output (intended and actual), and finally my code. Please point me toward the broken concept so I can figure out what I am missing. Thanks much.
DB info
I am using two tables, called Members and Payments, from one DB. Members has a primary key called ID and also has the fields first_name and last_name. Payments has a foreign key called Members_Id, which is associated to the Member's primary key; Payments also has the payment values under the column Payments.
Output should be like this
Member name = John Smith
$48.00, 10/20/2005
$44.00, 3/11/2006
But is this instead
Member name = SELECT ([t0].[First_Name] + #p1) + [t0].[Last_Name]
AS [value]FROM[dbo].[Members] AS [t0].[ID] = #p0
$48.00, 10/20/2005
$44.00, 3/11/2006
My VB Code
Public Class FormPaymentsGroup
Private db As New KarateClassesDataContext
Private Sub FormPaymentsGroup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.PaymentsTableAdapter.Fill(Me.KarateDataSet.Payments)
'Group payments by Member_ID (FKey) in the Payments table. (Working fine)
Dim IdQuery = From aMember In db.Payments
Group aMember By aMember.Member_Id
Into MemberPayments = Group
For Each memberID In IdQuery
' Use the passed member_Id to target the row in the Members table and return the first_name & last_name.
' PROBLEM: This only seems to be returning the query itself; not the result.
Dim currMemberID = memberID.Member_Id
Dim nameQuery = From aName In db.Members
Where aName.ID = currMemberID
Select aName.First_Name + " " + aName.Last_Name
Dim currName = nameQuery.ToString ' Load the query result into a portable variable.
LbxMemberPayments.Items.Add("Member name = " & currName) ' PROBLEM: This is where the name SHOULD BE posted to the listbox.
' This is bound to the Members table but directs it based on the above IdQuery.
For Each enteredPayment In memberID.MemberPayments
LbxMemberPayments.Items.Add(vbTab & FormatCurrency(enteredPayment.Amount) & ", " & enteredPayment.Payment_Date)
Next
LbxMemberPayments.Items.Add(vbCr) ' Carriage return formatting
Next
End Sub
End Class
change
Dim currName = nameQuery.ToString
to
Dim currName = nameQuery.FirstOrDefault

deleting a record in linq to sql (vb.net) what is wrong with my code?

I am getting the correct Employee Id in the VarEmpID variable. When I click on delete
It is giving me
Unable to cast object of type 'System.Data.Linq.DataQuery`1[my name space]' to type 'namespace'.
enter code here
Protected Sub radGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles radGrid1.DeleteCommand
Dim VarEmpId As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EmpId").ToString()
Using dc1 As New EmployeesDataClassesDataContext()
Dim EmployeeEntry = (From p In dc1.Employees
Where (p.EmpId = VarEmpId)
Select p)
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
dc1.SubmitChanges()
Dim queryResults = (From queryItem In EmployeeEntry Select queryItem).ToList()
If queryResults.Any Then
radGrid1.DataSource = queryResults
radGrid1.DataBind()
End If
End Using
End Sub
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
That method expects an Employee instance. Instead, you passed in an employee query.
Dim EmployeeEntry = ( query )
This is a query, not an entry. Consider calling Enumerable.First to get the first result of the query, and then deleting that.
Modified added Dim EmployeeEntry = (From p In dc1.Employees Where (p.EmpId = VarEmpId) Select p).singleorDefault() After that commented out the queryresults part and binded data again it solved my problem. – SmilingLily

customunboundcolumndata event not firing in DEVExpress gridview

I have been struggling with the below code snippet. "CustomUnboundColumnData" event is not firing.
Protected Sub gvAlertReport_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs)
If e.Column.FieldName = "Active" Then
Dim val As String = e.Value
Dim img As New ImageButton()
Select Case val
Case "False"
img.ImageUrl = "Images/gray_bell.png"
img.Attributes.Add("", "")
Case "True"
img.ImageUrl = "Images/red_bell.png"
img.Attributes.Add("", "")
Case Else
End Select
End If
End Sub
Thanks in Advance..
The CustomUnboundColumnData event is only triggered if there is a column in the ASPxGridView which is designed as an Unbound Column. To learn how this can be done, please refer to the Unbound Columns Overview topic. Also, please make certain that the gvAlertReport_CustomUnboundColumnData is the actual event handler for the ASPxGridView.

SSRS: Report loading external images, image not found, can I hide the image control

My SSRS report loads logo images for each customer from a customer number specific folder on the report server.
I write an expression, to form my URL to the image based on th customer number.
..."http://localhost/images/" + iCustomerNumber.ToString() + "/logo.gif"
I am able to get this working, but the problem I face is, when a particular customer doesn't has an image, then my report shows a red X mark in place of the logo. In this case, I expect to hide the image control itself. Any thoughts????
The other dirty solution will be to ensure that each customer specific folder has the designated image! even if there is no logo for a customer, I'll place a blank.gif or a spacer.gif of probably a square pixel in dimension!.
You could try adding some custom code and use this in the Image.Value property to load a default image if no image is found:
Public Function GetImage(ByRef CustomerNumber As String) As String
' Customer image
Dim ImageCustomerURL As String
ImageCustomerURL = "http://localhost/images/" + CustomerNumber + "/logo.gif"
' Default Image if customer image does not exist
Dim ImageDefaultURL As String
ImageDefaultURL = "http://localhost/images/default.gif"
' Create a web request to see if customer image exists
Dim m_Req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ImageCustomerURL)
Try
Dim HttpWResp As System.Net.HttpWebResponse = CType(m_Req.GetResponse(), System.Net.HttpWebResponse)
If HttpWResp.StatusCode = System.Net.HttpStatusCode.OK
Return ImageCustomerURL
Else
Return ImageDefaultURL
End If
Catch ex As System.Net.WebException
If ex.Status = System.Net.WebExceptionStatus.ProtocolError Then
Return ImageDefaultURL
End If
End Try
Return ImageDefaultURL
End Function
Then your Image.Value property expression is:
=Code.GetImage(iCustomerNumber.ToString())
Edit: Set the Visibility.Hidden property rather than use a default image
Well, I thought it might be nicer to have a default image rather than a blank space but it's really the same idea:
Public Function HideImage(ByRef CustomerNumber As String) As Boolean
' Customer image
Dim ImageCustomerURL As String
ImageCustomerURL = "http://localhost/images/" + CustomerNumber + "/logo.gif"
' Create a web request to see if customer image exists
Dim m_Req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ImageCustomerURL)
Try
Dim HttpWResp As System.Net.HttpWebResponse = CType(m_Req.GetResponse(), System.Net.HttpWebResponse)
If HttpWResp.StatusCode = System.Net.HttpStatusCode.OK
Return False
Else
Return True
End If
Catch ex As System.Net.WebException
If ex.Status = System.Net.WebExceptionStatus.ProtocolError Then
Return True
End If
End Try
Return True
End Function
Then your Visibility.Hidden property expression is:
=Code.HideImage(iCustomerNumber.ToString())

How to insert a data field into an empty table

I am making a front-end application in VB. The back-end is Oracle. I want an autogenerated ID on the click of a "New" button. It works well if data is present in the table, but shows an error if the table is empty. What do I need to insert so that it works when I am using the application for the first time? My button code is as follows:
Private Sub cmd_new_Click()
Call txt_clear
txt_name.Enabled = True
Set rsCat = New ADODB.Recordset
rsCat.Open "Category", conn, adOpenDynamic, adLockPessimistic
If rsCat.EOF = rscat.BOF Then
tempId = 1000
Else
rsCat.MoveLast
tempId = rsCat.Fields("Category_Id") + 1
End If
txt_Id = tempId
cmd_Save.Enabled = True
cmd_new = False
End Sub
check rscat.RecordCount=-1
Basically, change
If rsCat.EOF = rscat.BOF Then
to
If rsCat.RecordCount=-1 Then

Resources