sarang8180
asked on
Cannot fill datatable.Using VB.Net,SQL procedure
CODE OF VB.Net
dim dsUserLogin as new Dataset
daLogin.SelectCommand = GetSelectCommand(objConnec tion, UserName, Password)
.Fill(dsUserLogin,DSLogin. TestLoginT able)
End With
If dsUserLogin.Tables(0).Rows .Count = 1 Then
bol = True
Else
bol = False
End If
Private Function GetSelectCommand(ByRef objConnection As SqlConnection, ByVal strUser As String, ByVal strPassword As String) As SqlCommand
If bolUsernameCheck = True Then
cmdSelect = New SqlCommand("SP_CheckValid" , objConnection)
Else
cmdSelect = New SqlCommand("SP_CheckUserEx ist", objConnection)
End If
cmdSelect.CommandType = CommandType.StoredProcedur e
Try
With cmdSelect.Parameters
.Add(New SqlParameter(Param_UserNam e, strUser))
.Add(New SqlParameter(Param_Passwor d, strPassword))
GetSelectCommand = cmdSelect
If bolUsernameCheck = True Then
Dim i As Integer = cmdSelect.ExecuteScalar(). ToString()
GetSelectCommand = cmdSelect
Else
objConnection.Open()
cmdSelect.ExecuteNonQuery( )
Return cmdSelect
End If
End With
Catch ex As Exception
End Try
End Function
DSLogin.TestLoginTable is a class in which data columns of datatable dsUSerLogin are defined.
CODE OF SQL PROCEDURE
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select 0
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password )
select 1
end
GO
dim dsUserLogin as new Dataset
daLogin.SelectCommand = GetSelectCommand(objConnec
.Fill(dsUserLogin,DSLogin.
End With
If dsUserLogin.Tables(0).Rows
bol = True
Else
bol = False
End If
Private Function GetSelectCommand(ByRef objConnection As SqlConnection, ByVal strUser As String, ByVal strPassword As String) As SqlCommand
If bolUsernameCheck = True Then
cmdSelect = New SqlCommand("SP_CheckValid"
Else
cmdSelect = New SqlCommand("SP_CheckUserEx
End If
cmdSelect.CommandType = CommandType.StoredProcedur
Try
With cmdSelect.Parameters
.Add(New SqlParameter(Param_UserNam
.Add(New SqlParameter(Param_Passwor
GetSelectCommand = cmdSelect
If bolUsernameCheck = True Then
Dim i As Integer = cmdSelect.ExecuteScalar().
GetSelectCommand = cmdSelect
Else
objConnection.Open()
cmdSelect.ExecuteNonQuery(
Return cmdSelect
End If
End With
Catch ex As Exception
End Try
End Function
DSLogin.TestLoginTable is a class in which data columns of datatable dsUSerLogin are defined.
CODE OF SQL PROCEDURE
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select 0
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password
select 1
end
GO
ASKER
Why no body is answering?I need this answer.Urgently
try this
Dim strConnectionString As String = "connetion string here"
Dim objConnection As New SqlConnection(strConnectionString)
Dim cmdSelect As SqlClient.SqlCommand
If bolUsernameCheck = True Then
cmdSelect = New SqlCommand("SP_CheckValid", objConnection)
Else
cmdSelect = New SqlCommand("SP_CheckUserExist", objConnection)
End If
cmdSelect.CommandType = CommandType.StoredProcedure
cmdSelect.Parameters.Add("@Username ", SqlDbType.VarChar, 100).Value = Usernam
cmdSelect.Parameters.Add("@Password ", SqlDbType.VarChar, 100).Value = Password
If bolUsernameCheck = True Then
Dim i As Integer = cmdSelect.ExecuteScalar().ToString()
Else
objConnection.Open()
cmdSelect.ExecuteNonQuery()
objConnection.Close()
End If
ASKER
Actually, my stored procedure is not returning anything that is why datat table is not getting filled.
yes . then whats the problem ?
ASKER
i want stored procedure to return user name and password.Fill it in a datat table.how to do it in store d procedure
Dim strConnectionString As String = "connetion string here"
Dim objConnection As New SqlConnection(strConnectio nString)
Dim cmdSelect As SqlClient.SqlCommand
If bolUsernameCheck = True Then
cmdSelect = New SqlCommand("SP_CheckValid" , objConnection)
Else
cmdSelect = New SqlCommand("SP_CheckUserEx ist", objConnection)
End If
cmdSelect.CommandType = CommandType.StoredProcedur e
cmdSelect.Parameters.Add(" @Username ", SqlDbType.VarChar, 100).Value = Usernam
cmdSelect.Parameters.Add(" @Password ", SqlDbType.VarChar, 100).Value = Password
If bolUsernameCheck = True Then
Dim i As Integer = cmdSelect.ExecuteScalar(). ToString()
Else
Dim dacmd As New SqlClient.SqlDataAdapter(c mdSelec)
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
'Here you will ger dscmd.tables(0)
End If
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select username,password from UserLogin where upper(username)= upper(@username)
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password )
select username,password from UserLogin where upper(username)= upper(@username)
end
Dim objConnection As New SqlConnection(strConnectio
Dim cmdSelect As SqlClient.SqlCommand
If bolUsernameCheck = True Then
cmdSelect = New SqlCommand("SP_CheckValid"
Else
cmdSelect = New SqlCommand("SP_CheckUserEx
End If
cmdSelect.CommandType = CommandType.StoredProcedur
cmdSelect.Parameters.Add("
cmdSelect.Parameters.Add("
If bolUsernameCheck = True Then
Dim i As Integer = cmdSelect.ExecuteScalar().
Else
Dim dacmd As New SqlClient.SqlDataAdapter(c
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
'Here you will ger dscmd.tables(0)
End If
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select username,password from UserLogin where upper(username)= upper(@username)
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password
select username,password from UserLogin where upper(username)= upper(@username)
end
ASKER
how to catch return value 1 from sql stored procedure in vb.net.What parameters i have to give in vb.net
dod you mean you need username password as well as values o and 1 also ????
then try this
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select 0 as val , username,password from UserLogin where upper(username)= upper(@username)
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password )
select 1 as val ,username,password from UserLogin where upper(username)= upper(@username)
end
then try this
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username))
Begin
select 0 as val , username,password from UserLogin where upper(username)= upper(@username)
end
else
Begin
insert into userlogin(username,pwd) values(@username,@password
select 1 as val ,username,password from UserLogin where upper(username)= upper(@username)
end
and then on Front end
Dim dacmd As New SqlClient.SqlDataAdapter(c mdSelec)
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
after this
ds.Tables(0).Rows(0).Item( "val")
will return you 0 or 1
Dim dacmd As New SqlClient.SqlDataAdapter(c
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
after this
ds.Tables(0).Rows(0).Item(
will return you 0 or 1
ASKER
i don't want username and password.I want only value val
it is giving an error that it is not converting string value 'val' into integer
when I am writing
dim i as integer
i=convert.ToInt32(dsUserLo gin.Tables (1).Rows(0 ).Item("va l"))
it is giving an error that it is not converting string value 'val' into integer
when I am writing
dim i as integer
i=convert.ToInt32(dsUserLo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'am going on lunch.Will be back later.
ASKER
It is still showing error.Specified cast is not valid
ASKER
what to do?
Not working: .i=convert.ToInt32(dsUserL ogin.Table s(0).Rows( 0).Item("v al"))
Not working: .i=convert.ToInt32(dsUserL
ASKER
Specified cast is not valid.Error is showing
Can you check what value you rae getting for
dsUserLogin.Tables(0).Rows (0).Item(" val")
dsUserLogin.Tables(0).Rows
Dim strConnectionString As String = "connetion string here"
Dim objConnection As New SqlConnection(strConnectio nString)
Dim cmdSelect As SqlClient.SqlCommand
cmdSelect = New SqlCommand("SP_CheckUserEx ist", objConnection)
cmdSelect.CommandType = CommandType.StoredProcedur e
cmdSelect.Parameters.Add(" @Username ", SqlDbType.VarChar, 100).Value = Usernam
cmdSelect.Parameters.Add(" @Password ", SqlDbType.VarChar, 100).Value = Password
Dim dacmd As New SqlClient.SqlDataAdapter(c mdSelec)
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
Dim objConnection As New SqlConnection(strConnectio
Dim cmdSelect As SqlClient.SqlCommand
cmdSelect = New SqlCommand("SP_CheckUserEx
cmdSelect.CommandType = CommandType.StoredProcedur
cmdSelect.Parameters.Add("
cmdSelect.Parameters.Add("
Dim dacmd As New SqlClient.SqlDataAdapter(c
Dim dscmd As New DataSet
dacmd.Fill(dscmd)
ASKER
it's working fine .Thank you very much
ASKER
are u staying in mumbai?
no , in Pune. Can you please close the question
ASKER
are u staying in mumbai?
ASKER
Please tell me is there any thing logical error in this code.Because whenever I'am trying put new record in textbox.Value of val it is showing 0 only.
dsUserLogin.Tables(DSLogin .TestLogin Table).Row s(0).Item( "val")
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username) )
Begin
select 0 as val
End
else
begin
insert into UserLogin(username,pwd) values(@username,@password )
select 1 as val
end
GO
dsUserLogin.Tables(DSLogin
CREATE Procedure SP_CheckUserExist
@Username varchar(100),
@password varchar(100)
as
if exists(select * from UserLogin where upper(username)= upper(@username) )
Begin
select 0 as val
End
else
begin
insert into UserLogin(username,pwd) values(@username,@password
select 1 as val
end
GO
ASKER