Making use of service references in vb.net - asp.net

I've been trying to make use of this API in order to create a small app that will be run at a set interval in order to monitor a value. I've been told to do it in vb.net using service references and have so far managed to make use of the method that returns the last publication time, but when I try to retrieve the actual information from the table I receive an error:
"Exception: Compression is Not Enabled,This Web Service expects
clients to support GZIP,Deflate Compression"
So far i've been finding it difficult to find any info that is applicable to my current setup so i'm not sure how to progress. Is there anything i'm missing or any resources that would help me get my head around this?
Here is the current code i'm using to get the values.
Private Sub Soap(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim svc As New FlowInfo.InstantaneousFlowWebServiceSoapClient()
Dim str As String = svc.GetLatestPublicationTime().ToString
MessageBox.Show(str)
Dim body As FlowInfo.EDPReportBE = svc.GetInstantaneousFlowData()
End Sub

Related

Dim WithEvents Topic

Well,I am beginner in Vb.NET although I had started learning few years back then took break again started and again break. So,anyways I am still the beginner and I am unable to understand what this WithEvents actually does and how and when to be used?I was studying on Dim and came across Dim WithEvents. I tried finding articles but all of them got bit higher level programming code compared to my level so it's gonna take time for me to reach that level.Until then for now I want to know WithEvents actual usage. Can someone give me any simple program that can be the clarity of WithEvents?
Source Links :
Generic WithEvents
VB.NET: WithEvents not Working
Strange WithEvents thing
WithEvents+Handles+Overrides
in vb.net how do I declare a public variable from a private sub
https://msdn.microsoft.com/en-us/library/stf7ebaz(v=vs.90).aspx
Thank you.
In short, WithEvents tells VB that the object you are declaring can raise events and that you intend to handle those events. This keyword goes hand in hand with the Handles keyword.
Create a new form and then add a button to it. Then double-click the button. VB will generate code similar to the following (notice the Handles keyword):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
What you don't immediately see is the declaration for the button. If you look into the Form1.Designer.vb file you will see a line like this:
Friend WithEvents Button1 As System.Windows.Forms.Button
Notice the WithEvents. Since the button will raise events, the variable must be declared using that keyword.

How do I update an entity in EF across multiple ASP.NET requests without retrieving it again?

Sounds easy, right? Here's the scenario...
Private dbQuery As New ReefEntities
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CurrentCoral = (From c In dbQuery.Corals Where c.CoralID = intCoralID).FirstOrDefault
txtCommonName.Text = CurrentCoral.CommonName
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
'how do I access the coral from the page load to update it?
CurrentCoral.CommonName = strName
dbQuery.SaveChanges()
End Sub
I don't want to re-query my result, I want to update the query from the page load and then save the changes, right? How do I access that original object to update it?
HTTP is a stateless protocol and as a result, every request you make to your server needs to rebuild your object graph unless you persist it somewhere. There are many ways to persist data across a web "session". In ASP.NET you can store data in cookies, server side session, viewstate, form variables, and more.
First you would detach your CurrentCoral from the object context when you're done with it in Page_Load
dbQuery.Detach(CurrentCoral)
Then put it in a data store like view state.
Me.ViewState.Add("CurrentCoral", CurrentCoral)
In the next web request when your save button is clicked, retrieve the entity from the view state and attach it to your new object context.
CurrentCoral = CType(Me.ViewState("CurrentCoral"), Coral)
dbQuery.Attach(CurrentCoral)
CurrentCoral.CommonName = strName
dbQuery.SaveChanges()
Please forgive any syntax errors. VB.NET is not my first language! For more details on attaching and detaching entities with Entity Framework see the following article.
Attaching and Detaching Objects
You could put your CurrentCoral object into the ViewState or Session, then retrieve it in the Click event.
Dim oCurrentCorral as Object = ViewState("CurrentCoral")
dbQuery.ObjectStateManager.ChangeObjectState(oCurrentCoral, System.Data.EntityState.Added)
dbQuery.SaveChanges()
If the request is a postback, the record isn't loaded in Page_Load at all – there's nothing to re-query since you haven't made a query in the request at all. I'd say to just re-query in the postback handler, chances are the overhead of hitting your database once to update is much smaller than the overhead of sending the whole entity serialised in ViewState all the time.

Store OpenID users into my DB like Stackoverflow does

I have made a login system using OpenID using the following code:
<rp:OpenIdLogin
runat="server"
Identifier="https://www.google.com/accounts/o8/id"
Visible="true"
ExampleUrl=""
LabelText=" "
RegisterText="Register"
ExamplePrefix=" "
ID="OpenIdLogin1"
OnLoggedIn="OpenIdTextBox1_LoggedIn"
RequestEmail="Require"
RequestPostalCode="Request">
</rp:OpenIdLogin>
It takes the user to Google for authentication. I only want to store the user information like email, full name and sex in my DB.
I have written the following code to retrive email from Google but nothing is returned:
Imports System
Imports DotNetOpenAuth.OpenId.Extensions.AttributeExchange
Partial Class Food
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Session("FetchResponse") Is Nothing) Then
Return
End If
Dim fetchResponse As FetchResponse = CType(Session("FetchResponse"), FetchResponse)
Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email)
End Sub
Public Property Email() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
End Class
If you are using Open Id you can ask for certain information from the Service provider.So when it came back once user has authorize themself you can get the information being provided by OpenId service provider.
One of the good way to ask for additional information is to use Attribute exchange.
see here for details how you can make use of Attribute exchange for Google
Google OpenId and Attribute Exchange
Once you have those parameters being send back by Google all you need to extract them from the Request and save them in the DB for future use
OpenID protocol will share certain information of user so you have to make sure if this fit in to your system or not.

How to register data for validation using RegisterForEventValidation in asp.net

I would like to know the method of using RegisterForEventValidation in asp.net
My problem is this.
If I enable eventvalidation, then changing the controls using javascript and then posting the information back to the server later on throws up an error.
But If I disable event validation, the data present/selected in the controls is not available in the event handlers in code behind.
So, how should one resolve such issues?
Also, are there are any good articles that explain the issue and a resolution in detail? Tried googling. Came across many articles. But nothing that matched my expectations.
A small progress.
If I do the below, the event validation error goes away, but am not able to get the selected value in the code behind on button click (after selecting "1" in the dropdown, since for now I have registered only that value)
I get a runtime error - Unable to convert from string to double when I try accessing the selected value in drop down in code behind (The reason I believe is that no value is passed in the first place).
Any idea on what might be going wrong here!? Thanks!
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Page.ClientScript.RegisterForEventValidation(Me.ddldobddId.UniqueID, "1")
MyBase.Render(writer)
End Sub
Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
If CType(Me.ddldobddId.SelectedValue, Integer) = 0 -> Throws the error
End Sub
You've pretty much got it. The DropDownList.SelectedValue is always going to be a System.String type.
You must convert the String to an Integer in your btnId_Click method.
Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
Dim convertedSelectedValue As Integer
convertedSelectedValue = Convert.ToInt32( Me.ddldobddId.SelectedValue )
End Sub

How can I share a variable from sub routine to sub routine?

I have a sub routine where I pull in information from a database. I want to use variables from this sub routine in another sub routine. I tried making everything public but it doesn't want to be friendly and share.
Dim strEmail as String
Public Sub readDB()
strEmail = "whatever#doohikcy.com"
End Sub
Public Sub submit_btn(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(strEmail)
End Sub
The readDB would read the database and set the variables and do whatever it needs to do. Then when they submit the form it would e-mail the form to whatever the email address was.
Every time an asp.net page loads, the global variables are wiped out. You can avoid this by using Session variables.
Public Sub readDB()
Session("strEmail") = "whatever#doohikcy.com"
End Sub
Then:
Public Sub submit_btn(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(CStr(Session("strEmail")))
End Sub
Passing them as arguments is great, if the sequence of calls to the subs in question makes that feasible. If you literally need a variable that is visible to different subs in the same class (code-behind page in ASP.Net), what you are looking for is probably a private member variable. Declare it outside of any sub or function with the access modifier Private, and all the subs in the class will be able to access it.
Private _foo As String
The underscore is a convention that some people love, some hate. It comes in handy in VB if you want to define a property to expose the variable, where you can't use Foo as distinctly different from foo, but that's another story.
This is not the same as what would generally be understood by the term Global variable in the ASP.Net sense, where the variable would be visible throughout the application context, which lends itself to unintended consequences in the least. A private member variable is only visible to the class that owns it.
EDIT: Your example code was added after my initial answer. My VB is a little rusty but as you've written it, strEmail looks like it should have class-level visibility, including inside of submitbtn (someone correct me if I'm wrong). One possibility, since you mentioned that you are calling readDB in Page_Load is if you're checking for postback in page load, and only calling readDB on the initial load, not on postback, which would be the case when the button is clicked. You may have seen examples that include a check for Postback out of hand and not realized what it does (I only suggest that because you mentioned that you are new to ASP.Net and it's not intuitive if you're new to it--no offense intended).
Protected Sub Page_Load (sender as object, e as EventArgs)
If Not IsPostback
// this doesn't get called when the button is clicked so
// strEmail would not be populated when submitbtn is invoked
readDB
End If
End Sub
That's a bit of a guess out of nowhere though, so it might be way off base. Have you set break-points in Page_Load, readDB and submitbtn to see the state of strEmail in each?

Resources