Not Knowing How to Display Username After Login - vb.net

I’m still stumbling with this page over and over again. Just couldn’t get the user’s Username (using email as the username) to display on mysupport.aspx page after she’s successfully logged in. The result should look like this with the email showing but it is not retrieving anything:
Email: barb#hotmail.com
Being an amateur, I know I’m missing a big piece of the puzzle but I don’t know what. Am I using the mailLBL.Text = User.Identity.Name.ToString() wrongly? Below are the code-behind:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Partial Class mysupport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim sConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("TrackTicketsConnectionString2").ConnectionString)
sConnection.Open()
Dim cmdS As String = "Select Email from Users Where Deleted='N'"
Dim cmdCheckmail As New SqlCommand(cmdS, sConnection)
If Session("Ticket") IsNot Nothing Then
mailLBL.Text = User.Identity.Name.ToString()
Else
Response.Redirect("SignIn.aspx")
End If
End Sub
Protected Sub signinBTN_Click(ByVal sender As Object, ByVal e As EventArgs)
Session("Ticket") = Nothing
Response.Redirect("SignIn.aspx")
End Sub
End Class
Any help and guidance is truly appreciated!

What you could do first is study these links:
How to: Implement Simple Forms Authentication
How to use Sessions
There are several things wrong with this code.
Old Code:
Dim cmdS As String = "Select Email from Users Where Deleted='N'"
Dim cmdCheckmail As New SqlCommand(cmdS, sConnection)
If Session("Ticket") IsNot Nothing Then
mailLBL.Text = User.Identity.Name.ToString()
Else
Response.Redirect("SignIn.aspx")
End If
Corrected Code:
If Session("Ticket") Is Nothing Then
Response.Redirect("SignIn.aspx")
Else
Dim cmdS As String = "Select Email from Users Where Deleted='N' AND Username=#Username"
Dim cmdCheckEmail as new SqlCommand(cmdS, sConnection)
cmd.AddParameters(new SqlParameter("#UserName", SqlDbType.VarChar).Value = Session("Ticket")
Dim obj as Object = cmd.ExecuteScalar()
If obj isNot Nothing
mailLBL.Text = Convert.ToString(obj)
End If
End
I hope that helps.

Related

Sharepoint connection fails (Winforms, VB.Net)

I did a small development where it is necessary to connect to a SharePoint list, the development works well with my user, but when testing with another user who has the same privileges on the site, it sends the following error message
"The sign-in name or password does not match one in the Microsoft Account System. "
I already tried with other users that the site are owners, but I get the same result.
Can you help me see what is the error in the above?
This is my code.
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint
Imports System.Security
Imports System.Net
Public Class Form4
Dim siteUrl As String = "https://example.sharepoint.com/site/SiteExample/"
Dim context As New ClientContext(siteUrl)
Dim web As Web
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim userNameSP As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim secureString As SecureString = New NetworkCredential("", password).SecurePassword
Dim cred = New SharePointOnlineCredentials(userNameSP, secureString)
Dim clientContext As New ClientContext(siteUrl)
clientContext.Credentials = cred
Dim web As Web = clientContext.Web
Dim oWebsite As Web = clientContext.Web
Dim collList As ListCollection = oWebsite.Lists
Dim oList As List = collList.GetByTitle("Example Test")
clientContext.Load(oList)
clientContext.ExecuteQuery()
Dim query As CamlQuery = CamlQuery.CreateAllItemsQuery()
query.ViewXml = "<View Scope='RecursiveAll'><Query><ViewFields><FieldRef Name='Category/></ViewFields></Query></View>"
Dim AllItems As ListItemCollection = oList.GetItems(query)
clientContext.Load(AllItems)
clientContext.ExecuteQuery()
If AllItems.Count > 0 Then
... Do Something
end if
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Thank you in advance.
Did the user account enable MFA Authentication ?
I tested the same code snippet with a normal user account without MFA, it's working as expected.
Please check MFA Status in this:
https://account.activedirectory.windowsazure.com/UserManagement/MultifactorVerification.aspx?BrandContextID=O365
If enabling the MFA, please disable it, as CSOM didn't support MFA currently.
This is the solution code to do log-in in a sharepoint.
Imports Microsoft.Office.Interop
Imports System.IO
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint
Imports System.Security
Imports System.Net
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim siteUrl As String = "https://example.sharepoint.com/sites/TRAINING/"
Dim authManager = New OfficeDevPnP.Core.AuthenticationManager()
Dim ctx As ClientContext = authManager.GetWebLoginClientContext(siteUrl)
Dim web As Web = ctx.Web
ctx.Load(web, Function(w) w.Title)
ctx.ExecuteQuery()
Console.WriteLine("You have connected to {0} site, with Multi Factor Authentication enabled!!", web.Title)
Try
Do Something...
Catch ex As Exception
MessageBox.Show("An error occurred:" & Environment.NewLine & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sorry for the delay.
Regards.

vb.net 2017 unable to insert Calendar event

I have looked at all the examples I could find and hobbled together the code below. The first time I ran it a web page asked if I wanted to allow my user to update Google calendar and then it asked me to select an account. After that it just sat there and my windows application never finished painting the screen and displaying the simple button I had placed on a form from which to run the insert calendar event. Now when I run the code it comes up, a small gray window appears and it just sits there. What am I doing wrong?
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Calendar.v3.EventsResource
Imports Google.Apis.Services
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Util.Store
Imports Google.Apis.Requests
Public Class Form1
Dim service As CalendarService
Dim GRPS As IList(Of String) = New List(Of String)()
Sub New()
Authenticate()
End Sub
Private Function Authenticate()
GRPS.Add(CalendarService.Scope.Calendar)
Dim credential As UserCredential
Using stream As New FileStream("client_secret.json", FileMode.Open,
FileAccess.Read)
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets, GRPS, "user",
CancellationToken.None, New
FileDataStore("Vanguard")).Result
End Using
' Create the calendar service using an initializer instance
Dim initializer As New BaseClientService.Initializer()
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "Vanguard"
service = New CalendarService(initializer)
Return 0
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
InsertCalendar()
MessageBox.Show("Done")
Application.Exit()
End Sub
Public Sub InsertCalendar()
Dim CalendarEvent As New Data.Event
Dim SDT As New Data.EventDateTime
Dim A As Date = DateSerial(2017, 8, 11)
A = A.AddHours(10)
A = A.AddMinutes(30)
SDT.DateTime = A
Dim b As Date
b = A.AddHours(2)
Dim EDT As New Data.EventDateTime
EDT.DateTime = b
CalendarEvent.Start = SDT
CalendarEvent.End = EDT
CalendarEvent.Id = System.Guid.NewGuid.ToString
CalendarEvent.Description = "Test Event"
Dim list As IList(Of CalendarListEntry) =
service.CalendarList.List().Execute().Items()
service.Events.Insert(CalendarEvent, list(0).Id).Execute()
End Sub
End Class

End of statement expected. - VB Code to WebService Error

I'm getting this error when trying to debug my VB code in Visual Studio to interact with a specific WebService .
Im not very familiar with Visual Basic.
The error is on the line Dim ticket_handle As String = " CR 1001 " ws.closeTicket ( Sid , " closed ticket " ticket_handle )
The complete code:
Imports System.IO Imports System.Xml
Imports System.Xml.Serialization
Imports WebReference
Partial Class _Default
Inherits System.Web.UI.Page
Dim ws As New USD_WebService
Dim sid As String
Dim userhandle, username, password As String
Dim attrVal(5), attr(0), prop(0) As String
Dim requestHandle, requestNumber As String
Dim persistent_id As String
Dim catAttrib(5) As String
Sub Main()
Dim ws As New USD_WebService
ws.Url = "http://hummer:8080/axis/services/USD_R11_WebService?wsdl"
Dim username, password
Dim sid
username = "servicedesk"
password = "password"
sid = ws.login(username, password)
Dim userhandle
userhandle = ws.getHandleForUserid(sid, username)
Dim USD As New WebReference.USD_WebService
sid = USD.login(username, password)
Dim ticket_handle As String = “cr:1001” ws.closeTicket(Sid,“ticket fechado”, ticket_handle)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ws.Url = "http://hummer:8080/axis/services/USD_R11_WebService?wsdl"
End Sub
End Class
Can anyone help me plis?!?!
In VB.NET, only one statement may be executed per line. Other languages, like Java or C# use a ';' to denote the end of a statement, however in VB.NET it is the end of a line. The compiler is trying to tell you that you have two statements on a single line and it expects there to only be one.
Dim ticket_handle As String = “cr:1001” ws.closeTicket(Sid,“ticket fechado”, ticket_handle)
Should be
Dim ticket_handle As String = “cr:1001”
ws.closeTicket(Sid,“ticket fechado”, ticket_handle)

Sql Statement Custom Class

I am new to programming, I tried to create a sub that contains a SQL statement in it using parameterized query. My code is not working, there might something lacking to it or am I doing it wrong.
My connection string is in the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name = "DBCS"
connectionString ="Data Source=Mic-PC\Developer; Initial Catalog=Customer; User Id = sa; Password=1224334;"
providerName ="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I create a class called SELECTCLASS and here is the content
Imports System.Configuration
Imports System
Imports System.Threading.Tasks
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class SelectClass
Public Sub searchData(search As String, LastName As String, FirstName As String, MiddleInitial As String, Age As String, Address As String)
Dim CS As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Using con As New SqlConnection(CS)
Dim cmd As SqlCommand = New SqlCommand("spGetCustomerByName", con)
cmd.CommandType = CommandType.Storedprocedure
cmd.Parameters.AddWithValue("#FirstName", "%" + search + "%")
con.Open()
Using rdr = cmd.ExecuteReader()
If rdr.HasRows Then
While rdr.Read()
LastName = rdr.GetString(1).ToString
FirstName = rdr.GetString(2).ToString
MiddleInitial = rdr.GetString(3).ToString
Age = rdr.GetString(4).ToString
Address = rdr.GetString(5).ToString
End While
End If
End Using
End Using
End Sub
End Class
And here is the code for my Windows Form:
Imports System.Configuration
Imports System
Imports System.Threading.Tasks
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class SELECTVB
Dim SelectData As New SelectClass
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
SelectData.searchData(txtsearch.Text, TextBox1.Text.ToString, TextBox2.Text.ToString, TextBox3.Text.ToString, TextBox4.Text.ToString, TextBox5.Text.ToString)
End Sub
Any help is greatly appreciated, thank you in advance!
I think I see two problem with your code, neither of which have anything to do with ADO or SQL. The problems are related to the way that the data that is read from the database is being passed back to the calling method. I suspect that the searchData method is successfully reading the data from the query results (though you would need to confirm this using the debugger by setting a break-point, stepping through the method, and inspecting the variables' values).
The first problem I see is that you are passing the result of the TextBox.Text.ToString method rather than the TextBox.Text property itself. In other words, you should be calling the searchData method like this:
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
SelectData.searchData(txtsearch.Text, TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
End Sub
Secondly, you are attempting to reassign the reference of those Text arguments from inside the searchData method, but the parameters are not declared ByRef. If you want to do it that way, you would need to change the parameters of the method to ByRef, like this:
Public Sub searchData(search As String, ByRef LastName As String, ByRef FirstName As String, ByRef MiddleInitial As String, ByRef Age As String, ByRef Address As String)
However, none of that would be a problem if you returned the results via the function's return value rather than changing the values of the parameters. Most developers, including myself, discourage using ByRef parameters in this manner because it's unexpected behavior. Typically parameters should be reserved for inputs rather than outputs. For instance, if you created a class like this:
Public Class Customer
Public Property LastName As String
Public Property FirstName As String
Public Property MiddleInitial As String
Public Property Age As String
Public Property Address As String
End Class
Then you could change your search method to return a Customer object, like this:
Public Function SearchData(search As String) As Customer
Dim result As Customer = Nothing
Dim CS As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Using con As New SqlConnection(CS)
Using cmd As SqlCommand = New SqlCommand("spGetCustomerByName", con)
cmd.CommandType = CommandType.Storedprocedure
cmd.Parameters.AddWithValue("#FirstName", "%" & search & "%")
con.Open()
Using rdr = cmd.ExecuteReader()
If rdr.HasRows Then
If rdr.Read() Then
result = New Customer()
result.LastName = rdr.GetString(1)
result.FirstName = rdr.GetString(2)
result.MiddleInitial = rdr.GetString(3)
result.Age = rdr.GetString(4)
result.Address = rdr.GetString(5)
End If
End If
End Using
End Using
End Using
Return result
End Sub
And then you could call it like this:
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
Dim c As Customer = SelectData.searchData(txtsearch.Text)
If c IsNot Nothing Then
TextBox1.Text = c.LastName
TextBox2.Text = c.FirstName
TextBox3.Text = c.MiddleInitial
TextBox4.Text = c.Age
TextBox5.Text = c.Address
End If
End Sub
However, given the context of what you are doing, it seems likely that your method ought to be returning a list of customers rather than just a single one.
If making these changes still does not fix the problem, stepping through the search method with the debugger will be your best chance of solving the problem. It could be that the stored procedure is returning no matches, or it's returning a different number of columns than you are expecting, or it could be returning a column with a data-type other than a string (like the age column might be an integer, for instance). It's impossible to say, given only the information that you've provided, what the precise problem with the data access code would be.

How can I get the events for one day (Google calendar Api) using TimeMax and TimeMin in net?

I have a code for get a event list of calendar but retrieves ALL events. How can I get the events for just ONE day?
Here is the code that Im using:
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports System.Collections.Generic
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Calendar.v3.Data.Event
Imports Google.Apis.Util.Store
Imports Google.Apis.Calendar.v3.EventsResource
Imports System.Threading
Public Class Form1
Dim scopes As IList(Of String) = New List(Of String)
Dim service As CalendarService
Dim initializer = New BaseClientService.Initializer
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim service As CalendarService
Try
scopes.Add(CalendarService.Scope.Calendar)
Dim Secrets = New ClientSecrets()
Secrets.ClientId = "CLIENT ID"
Secrets.ClientSecret = "CLIENT SECRET"
Dim scope = New List(Of String)
scope.Add(CalendarService.Scope.Calendar)
Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scopes, "xxxxxxxxxxx#developer.gserviceaccount.com", CancellationToken.None).Result()
Dim initializer = New BaseClientService.Initializer
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "APP NAME"
service = New CalendarService(initializer)
Dim list As IList(Of CalendarListEntry) = service.CalendarList.List().Execute().Items
Dim requeust As ListRequest = service.Events.List("MY EMAIL")
Me.DataGridView1.DataSource = requeust.Execute.Items
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Try to add your searching range with these few lines of code
requeust.TimeMax = Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK")
requeust.TimeMin = "2014-12-01T16:00:00" 'changing the time whenever you want
I think it's work :)