display a warning message when an exception occurs - vb.net

i have a webservice that inserts, delete, updates the data from or to a database. so in this particular form it has a foreign key. so basically it should only accept values which are present in the primarty table.
when user enters any other value a exception occurs.
How do I make sure that such type of SQL exception when caught, a user friendly custom error message(Such as the entered ID doesn't exist.Data insertion failed!) is passed from my web service.
Thanks a lot.
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class WebService2
Inherits System.Web.Services.WebService
Public Class Details
Public Property CardID As Integer
Public Property BookID As Integer
Public Property IssuedBy As String
End Class
<WebMethod>
Public Sub AddBook(ByVal emp As Details)
Dim cs1 As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con1 As SqlConnection = New SqlConnection(cs1)
Dim thequery As String = "select * from BookIssue where CardID=#CardID"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, con1)
cmd1.Parameters.Add(New SqlParameter() With {
.ParameterName = "#CardID",
.Value = emp.CardID
})
con1.Open()
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MsgBox("ID Number is Already in use")
con1.Close()
Else
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spInsertIntoBookIssue", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#CardID",
.Value = emp.CardID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#BookID",
.Value = emp.BookID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#IssuedBy",
.Value = emp.IssuedBy
})
con.Open()
cmd.ExecuteNonQuery()
MsgBox("Data Inserted")
End Using
End If
End Using
End Sub
<WebMethod>
Public Sub GetAllDetails()
Dim listEmployees As List(Of Details) = New List(Of Details)()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("Select * from BookIssue", con)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim details As Details = New Details()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
listEmployees.Add(details)
End While
End Using
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Context.Response.Write(js.Serialize(listEmployees))
End Sub
<WebMethod>
Public Sub GetAllDetails1()
Dim listEmployees As List(Of Details) = New List(Of Details)()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("Select * from BookIssue", con)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim details As Details = New Details()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
listEmployees.Add(details)
End While
End Using
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Context.Response.Write(js.Serialize(listEmployees))
End Sub
<WebMethod>
Public Function GetdetailsById(ByVal CardID As Integer) As Details
Dim details As Details = New Details()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spGetBookIssueDetailsByCardID", con)
cmd.CommandType = CommandType.StoredProcedure
Dim parameter As SqlParameter = New SqlParameter()
parameter.ParameterName = "#CardID"
parameter.Value = CardID
cmd.Parameters.Add(parameter)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
End While
End Using
Return details
End Function
<WebMethod>
Public Function GetdetailsById1(ByVal CardID As Integer) As Details
Dim details As Details = New Details()
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spGetBookIssueDetailsByCardID1", con)
cmd.CommandType = CommandType.StoredProcedure
Dim parameter As SqlParameter = New SqlParameter()
parameter.ParameterName = "#CardID"
parameter.Value = CardID
cmd.Parameters.Add(parameter)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
details.CardID = Convert.ToInt32(rdr("CardID"))
details.BookID = Convert.ToInt32(rdr("BookID"))
details.IssuedBy = rdr("IssuedBy").ToString()
End While
End Using
Return details
End Function
<WebMethod>
Public Sub DeleteRecord1(ByVal emp As Details)
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spDeleteByID", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#CardID",
.Value = emp.CardID
})
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
<WebMethod>
Public Sub Update(ByVal emp As Details)
Dim cs As String = ConfigurationManager.ConnectionStrings("library management systemConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand("spUpdateBookIssue", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#CardID",
.Value = emp.CardID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#BookID",
.Value = emp.BookID
})
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#IssuedBy",
.Value = emp.IssuedBy
})
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Class

I suggest modeling your code off this example, i modified the delete sub you provided to include a catch with a custom ErrorRoutine for unhandled cases, otherwise you can obtain the number of deleted rows from the executeNonQuery() function. I also chnaged so we return the number of deleted rows, this way if its less than 1 you can do whatever you want with messages after calling the delete.
Public Sub DeleteRecord1(ByVal emp As Details) As Integer
Dim cs As String = ConfigurationManager.ConnectionStrings("library management
systemConnectionString").ConnectionString
Dim m_RowsDeleted As Integer
Dim con As SqlConnection = New SqlConnection(cs)
Try
Using con
Dim cmd As SqlCommand = New SqlCommand("spDeleteByID", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter() With {
.ParameterName = "#CardID",
.Value = emp.CardID
})
con.Open()
m_RowsDeleted = cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
ErrorRoutine(ex, "Delete")
Finally
If Not IsNothing(con) Then
If con.State = ConnectionState.Open Then
con.Close()
End If
End If
End Try
Return m_RowsDeleted
End Sub

Related

Call Oracle function in package in vb.net

I just begin with Oracle. I read lot of post and ... I don't find where is my mistake
I've a function in package PKG_GETOBJ
FUNCTION GetNewID(sReference IN VARCHAR2) RETURN NUMBER
IS
NewCtpVal NUMBER;
BEGIN
UPDATE TABLE set VALUE = (VALUE + 1) WHERE Field= sReference RETURN VALUE INTO NewCtpVal;
return NewCtpVal;
END;
and here how I try to call it:
Dim con As OracleConnection = GetConnexion()
con.Open()
Dim cmd As New OracleCommand With {.Connection = con}
'PKG_GETOBJ.GETNEWID('ID_OBJECT');'Sample calling in SQL with success
cmd.Parameters.Add(New OracleParameter With {.ParameterName = "sReference", .OracleDbType = OracleDbType.Varchar2, .Value = "ID_OBJECT", .Direction = ParameterDirection.Input})
cmd.Parameters.Add(New OracleParameter With {.ParameterName = "NewCtpVal", .OracleDbType = OracleDbType.Double, .Direction = ParameterDirection.ReturnValue})
cmd.CommandText = "PKG_GETOBJ.GETNEWID"
cmd.CommandType = CommandType.StoredProcedure
cmd.ExecuteNonQuery()
For Each p As OracleParameter In cmd.Parameters
HelperJournal.WriteEntry("PKG_GETOBJ " & p.ParameterName, If(p.Value Is Nothing, "null", p.Value.ToString))'Write in file
Next
con.Close()
con.Dispose()
My problem is NewCtpVal is empty, then I Database I've a number value...
Finnally I found a way...
Public Function GetNewId() As Decimal
Dim retval
Using con As OracleConnection = GetConnexion(),
cmd As New OracleCommand With {.Connection = con}
cmd.CommandText = "PKG_GETOBJ.GETNEWID"
cmd.CommandType = CommandType.StoredProcedure
Dim prm = New OracleParameter("returnvalue", OracleDbType.Decimal)
prm.Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add(prm)
Dim prm1 = New OracleParameter("sReference", OracleDbType.Varchar2)
prm1.Direction = ParameterDirection.Input
prm1.Value = "ID_OBJECT"
cmd.Parameters.Add(prm1)
con.Open()
cmd.ExecuteNonQuery()
retval = cmd.Parameters("returnvalue").Value.ToString
End Using
Return retval
End Function

Update table in sql database from listview items

Hello folks am trying read from the table identify a specific column if not YES then update my table using items from listview
Public Sub FeesFromSetFees(lst As ListView, Amt As String, Year As String, Clss As String, Term As String, Mode As String)
Dim txtID As New TextBox
Dim txtbal As New TextBox
Dim toText As New TextBox
Dim add As New TextBox
Try
con = New SqlConnection(My.Settings.DeseretConnectionString)
con.Open()
sql = "SELECT * FROM Fees"
command = New SqlCommand(sql, con)
reader = command.ExecuteReader
While reader.Read()
toText.Text = reader.Item("scholarship").ToString
If toText.Text.ToUpper <> "YES" Then
txtID.Text = reader.Item("id").ToString
add.Text = reader.Item("balance").ToString
txtbal.Text = CType(Amt.Trim, Double) + CType(add.Text.Trim, Double)
Dim item As New ListViewItem(txtbal.Text)
item.SubItems.Add(txtID.Text)
lst.Items.Add(item)
Dim lstId As New List(Of String)
Dim lstBalance As New List(Of String)
For Each li As ListViewItem In lst.Items
lstId.Add(li.SubItems(0).ToString)
lstBalance.Add(li.SubItems(1).ToString)
Next
Dim Sql = "Update fees Set class = #Class, year = #Year, mode = #Mode,term = #Term, balance = #Balance where id = #ID"
Using cn As New SqlConnection(My.Settings.DeseretConnectionString)
Using cmd As New SqlCommand(Sql, cn)
With cmd.Parameters
.Add("#Class", SqlDbType.VarChar).Value = Clss
.Add("#Year", SqlDbType.VarChar).Value = Year
.Add("#Mode", SqlDbType.VarChar).Value = Mode
.Add("#Term", SqlDbType.VarChar).Value = Term
.Add("#Balance", SqlDbType.VarChar)
.Add("#ID", SqlDbType.VarChar)
End With
cn.Open()
For index = 0 To lstId.Count - 1
cmd.Parameters("#Balance").Value = lstBalance(index)
cmd.Parameters("#ID").Value = lstId(index)
cmd.ExecuteNonQuery()
Next
End Using
End Using
MessageBox.Show("successful")
End If
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
I get my successful message but nothing really happen to data in the table
Public Sub FeesFromSetFees(Amt As String, Year As String, Clss As String, Term As String, Mode As String)
Dim txtID As New TextBox
Dim txtbal As New TextBox
Dim toText As New TextBox
Dim add As New TextBox
Try
con = New SqlConnection(My.Settings.DeseretConnectionString)
con.Open()
sql = "SELECT * FROM Fees"
command = New SqlCommand(sql, con)
reader = command.ExecuteReader
While reader.Read()
toText.Text = reader.Item("scholarship").ToString
If toText.Text.ToUpper <> "YES" Then
txtID.Text = reader.Item("id").ToString
add.Text = reader.Item("balance").ToString
txtbal.Text = CType(Amt.Trim, Double) + CType(add.Text.Trim, Double)
Dim Sql = "Update fees Set class = #Class, year = #Year, mode = #Mode,term = #Term, balance = #Balance where id = #ID"
Using cn As New SqlConnection(My.Settings.DeseretConnectionString)
Using cmd As New SqlCommand(Sql, cn)
With cmd.Parameters
.Add("#Class", SqlDbType.VarChar).Value = Clss
.Add("#Year", SqlDbType.VarChar).Value = Year
.Add("#Mode", SqlDbType.VarChar).Value = Mode
.Add("#Term", SqlDbType.VarChar).Value = Term
.Add("#Balance", SqlDbType.VarChar).Value = txtbal.Text
.Add("#ID", SqlDbType.VarChar).Value = txtID.Text
cn.Open()
cmd.ExecuteNonQuery()
End With
End Using
End Using
MessageBox.Show("successful")
End If
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

How to make a default item for ComboBox with a DataSource

In my application I have a data bound ComboBox that looks like this:
Dim listaCategoria As List(Of Ccategoria) = CAD.ObterTodosC()
cbxAlterarCg.DataSource = listaCategoria
cbxAlterarCg.DisplayMember = "nomeCategoria"
cbxAlterarCg.ValueMember = "idCategoria"
The class code (CAD):
Public Shared Function ObterTodosC() As List(Of Ccategoria)
Dim lstTodos As List(Of Ccategoria) = New List(Of Ccategoria)
Try
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = myDAC._connectionString
Using cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select * from Categoria"
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim p As Ccategoria = New Ccategoria()
p.IdCategoria = dr.GetInt32(0)
p.NomeCategoria = dr.GetString(1)
lstTodos.Add(p)
End While
End Using
End Using
Catch ex As SqlException
Throw ex
Catch ex As Exception
Throw ex
End Try
Return lstTodos
End Function
And the attributes:
Public Class Ccategoria
Private _idCategoria As Integer
Private _nomeCategoria As String
(...)
My ComboBox displays "nomeCategoria" with the "idCategoria" value right.
Now my question, like the title says can I create a default read only item so saying ("select your category") or something like that?
I've seen some other tutorials but none of them with ComboBox which are data bounded.
What you could do is add a Ccategoria() to lstTodos just under Dim lstTodos As List(Of Ccategoria) = New List(Of Ccategoria) like so:
Public Shared Function ObterTodosC() As List(Of Ccategoria)
Dim lstTodos As List(Of Ccategoria) = New List(Of Ccategoria)
Dim p As Ccategoria = New Ccategoria()
p.IdCategoria = 0
p.NomeCategoria = "select your category"
lstTodos.Add(p)
Try
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = myDAC._connectionString
Using cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select * from Categoria"
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim p As Ccategoria = New Ccategoria()
p.IdCategoria = dr.GetInt32(0)
p.NomeCategoria = dr.GetString(1)
lstTodos.Add(p)
End While
End Using
End Using
Catch ex As SqlException
Throw ex
Catch ex As Exception
Throw ex
End Try
Return lstTodos
End Function
In doing this you are simply adding to the DataSource. You could then look into providing validation by checking to see if idCategoria is 0. If it is you may want to consider showing a MessageBox to the user prompting them to pick a category.

How to refresh datagridview without refresh button?

How can I automatically refresh and view the new current datagridview after add or delete?
what code should I put after "msgbox" to view the current data?
Private Sub add()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim sSQL As String = String.Empty
Try
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "INSERT INTO course ( code, description)"
sSQL = sSQL & " VALUES (#cod, #des)"
cmd.CommandText = sSQL
cmd.Parameters.Add("#cod", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtcode.Text)) > 0, Me.txtcode.Text, DBNull.Value)
cmd.Parameters.Add("#des", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtdescription.Text)) > 0, Me.txtdescription.Text, DBNull.Value)
cmd.ExecuteNonQuery()
MsgBox("Data has been save.")
conn.Close()
Catch ex As Exception
MessageBox.Show("Already exist")
End Try
End Sub
You should bind your DataGridView to an appropriate DataSource, e.g. a DataTable.
Then, instead of inserting the data manually in the database, you could use a SqlDataAdapter and the DataTable your DataGridView is bound to.
Here's a simple example:
Dim cons = New SqlConnectionStringBuilder() With
{
.DataSource = "your_server",
.InitialCatalog = "your_db",
.UserID = "your_user",
.Password = "your_password"
}.ConnectionString
Dim con = New SqlConnection(cons)
con.Open()
' create a SqlDataAdapter and provide the SELECT command '
Dim adapter = New SqlDataAdapter()
Dim cb = New SqlCommandBuilder(adapter)
adapter.SelectCommand = New SqlCommand("SELECT code, description FROM course", con)
' the INSERT command can be generated '
adapter.InsertCommand = cb.GetInsertCommand()
' fill a new DataTable with data from database '
Dim dt = New DataTable()
adapter.Fill(dt)
' create a Form with DGV and Insert-Button '
Dim f = New Form()
Dim dgv = New DataGridView() With
{
.DataSource = dt,
.Dock = DockStyle.Fill
}
Dim addButton = New Button() With
{
.Text = "Add new",
.Dock = DockStyle.Bottom
}
Dim i = 0
AddHandler addButton.Click, Function(s, o)
' we insert the new data directly into the DataTable '
dt.Rows.Add(New Object() {"Some","Text"})
' and let the SqlDataAdapter handle the insert '
adapter.Update(dt)
End Function
f.Controls.Add(addButton)
f.Controls.Add(dgv)
f.ShowDialog()
Since the new data is written directly into the DataTable, the DataGridView is updated immediately.
Of course this is even easier when you use TableAdapters.

How do I pull a GUID from my database in order to Update a Username

I have a SQL database that is different than the one in ASP.net so I had to create a custom membership Provider. The provider to get the user looks like this:
My SQL class name is CustomSqlProvider:
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As MembershipUser
'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#UserName", SqlDbType.NVarChar)
cmd.Parameters.Add("#Password", SqlDbType.NVarChar)
Dim u As MembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
updateCmd.Parameters.Add("#UserName", SqlDbType.NVarChar)
updateCmd.Parameters.Add("#F_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("#L_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("#PWD", SqlDbType.VarChar)
updateCmd.Parameters.Add("#Email", SqlDbType.VarChar)
updateCmd.ExecuteNonQuery()
End If
Catch e As SqlException
'If WriteExceptionsToEventLog Then
' WriteToEventLog(e, "Get_User, as String")
' Throw New ProviderException(exceptionMessage)
'Else
'Throw e
'End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
Public Overrides Function GetUser(ByVal providerUserKey As Object, _
ByVal userIsOnline As Boolean) As MembershipUser
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#UserId", SqlDbType.UniqueIdentifier).Value = providerUserKey
Dim u As MembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
u = GetUserFromReader(reader)
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
updateCmd.Parameters.Add("#UserId", SqlDbType.UniqueIdentifier)
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
If WriteExceptionsToEventLog Then
WriteToEventLog(e, "GetUser(Object, Boolean)")
Throw New ProviderException(exceptionMessage)
Else
Throw e
End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
Now what I want to do is I first login to my website, it authenticates, and I am in. I am then redirected to another page where I can change my Username. But I can't seem to get the GUID of the logged in User in order to change it. I'm using a stored procedure that Updates the Users Table.
I have the following code on the Page where I can change credentials:
Dim currentUser as MembershipUser = Membership.GetUser()
Dim CurrentUSerId as Guid = CType(currentUser.ProviderUserKey, Guid)
I get reference not set to an instance of an object. Any help would be appreciated.
Thank you for the edit. I have also added get and set properties so I can be able to add values to those parameters. The parameters in the Update_User sproc are inputs. My Get_User sproc looks like this..which is why I think it is not getting the GUID because when it is run it asks for Username and password and then it returns everything including the Guid back:
GO
/****** Object: StoredProcedure [dbo].[Get_User] Script Date: 07/14/2010 09:16:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_User]
#UserName nvarchar(50),
#Password varchar(50)
AS
SELECT USER_ID, USER_NAME, F_NAME, L_NAME, BUILDING_ID, SIP_ROLE, INTERNAL_ID, PWD, EMAIL FROM dbo.USERS WHERE USER_NAME = #UserName AND PWD = #Password
RETURN
///////////////////////////////////////////////////////////////////////////////////////////
After adding those gets and sets and assigning parameter variables to my Sproc parameters I get the following error:
Reference not set to an instance of an object.
at this line of code:
Dim currentUser As MembershipUser = Membership.GetUser()
Dim UserId As Guid = CType(currentUser.ProviderUserKey, Guid)
And this is what I have on the UserInfo.aspx.vb Page under the Button click:
Dim sql As New SqlClient.SqlCommand("Update_User", con)
sql.Parameters.Add("#UserId", SqlDbType.UniqueIdentifier).Value = UserId
sql.Parameters.Add("#UserName", SqlDbType.NVarChar).Value = txtUserName.Text
In the first GetUser OverRide where does it execute the following line:
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
I can't see that the command is executed, I have been known to be incredibly dumb though :)
Perhaps your code needs to follow the pattern in the Get_User by ProviderUserKey pattern, I've added some missing things and questions, hope this helps.
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As MembershipUser
'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#UserName", SqlDbType.NVarChar).Value = "??????"
cmd.Parameters.Add(("#Password", SqlDbType.NVarChar).Value = "??????"
Dim u As MembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
u = GetUserFromReader(reader)
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
'Are these input or output parameters?
updateCmd.Parameters.Add("#UserName", SqlDbType.NVarChar).Value = u.UserName
updateCmd.Parameters.Add("#F_Name", SqlDbType.NVarChar).Value = "??????"
updateCmd.Parameters.Add("#L_Name", SqlDbType.NVarChar).Value = "??????"
updateCmd.Parameters.Add("#PWD", SqlDbType.VarChar).Value = "??????"
updateCmd.Parameters.Add("#Email", SqlDbType.VarChar).Value = "??????"
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
'If WriteExceptionsToEventLog Then
' WriteToEventLog(e, "Get_User, as String")
' Throw New ProviderException(exceptionMessage)
'Else
'Throw e
'End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function