I
IvicaNesic
Guest
Greetings community,
Greetings community,
First, my apology for not putting the code in the code window, but when I try to do this with more than one code window, posting doesn’t work. I tried to mix one SQL code window and two VB.NET code windows, but posting simply crashed.
This is part of bigger app, but I’ve created simple example on SQL server express 2012: database (DBQueriesProbe) of only one table (dbo.LookUP) and two functions (GetIDFromName and NameExist).
I populated the table with: Item01, Item02, Item03.
Then, I opened VS started new winform project. I added new data source from that database selecting only my table and those functions. I built the app, opened data set designer and got my table with its table adapter and another rectangle named QueriesTableAdapter with my two functions.
CREATE TABLE [dbo].[LookUP](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_LookUP] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE FUNCTION [dbo].[GetIDFromName](@name nvarchar(50))
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar int
-- Add the T-SQL statements to compute the return value here
SELECT @ResultVar = ID from dbo.LookUP where Name=@Name
if @@ROWCOUNT=0 set @ResultVar=0
-- Return the result of the function
RETURN @ResultVar
END
CREATE FUNCTION [dbo].[NameExist](@name as nvarchar(50))
RETURNS bit
AS
BEGIN
DECLARE @ResultVar bit
Declare @SelTable as table (ID int,Name nvarchar(50))
insert into @SelTable select * from dbo.LookUP where Name=@Name
if @@ROWCOUNT>0 set @ResultVar=1 else set @ResultVar=0
RETURN @ResultVar
END
GO
I right-clicked on LookUPTableAdapter, and selected preview data. Results were expected. When I tried to do the same with functions, preview for them was not available. Not knowing any better, I added another query in Queries table adapter, creating it from stored procedure GetIDFromName, only changing its name to fGetIDFromName, because you cannot have two queries with the same name. This new query appeared in preview menu, but as function of dataset not as function of QueriesTableAdapter. This new query returned result as expected.
But now, I come to interesting part. I didn’t want to bind data to something on the form. I wanted to try those functions. So I added new module to the project (DataManagement) with this code.
Module DataManagement
Public dsQP As New DBQueriesProbeDataSet
Public taLookUP As New DBQueriesProbeDataSetTableAdapters.LookUPTableAdapter
Public taQueries As New DBQueriesProbeDataSetTableAdapters.QueriesTableAdapter
Public tamQP As New DBQueriesProbeDataSetTableAdapters.TableAdapterManager
Public bsLookUP As New BindingSource
Public Sub InitDataManagement()
tamQP.LookUPTableAdapter = taLookUP
taLookUP.Fill(dsQP.LookUP)
bsLookUP.DataSource = dsQP
bsLookUP.DataMember = dsQP.LookUP.TableName
End Sub
End Module
On the form I just added one multiline text box to show the results. In form load event I added this code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitDataManagement()
Dim sMess As String = ""
bsLookUP.MoveFirst()
Dim CurLU = CType(CType(bsLookUP.Current, DataRowView).Row, DBQueriesProbeDataSet.LookUPRow)
sMess = CurLU.Name & vbCrLf
sMess &= CInt(taQueries.GetIDFromName(CurLU.Name)).ToString & vbCrLf
sMess &= CInt(taQueries.fGetIDFromName(CurLU.Name)).ToString & vbCrLf
sMess &= CBool(taQueries.NameExist(CurLU.Name)).ToString
TextBox1.Text = sMess
End Sub
End Class
And got this as a result
Item01
1
0
True
And this shows that functions from QueriesTableAdapter added by dataset designer worked, but the one added manually didn’t work, even though it was created from the same “stored” function.
Can anyone explain this?
Continue reading...
Greetings community,
First, my apology for not putting the code in the code window, but when I try to do this with more than one code window, posting doesn’t work. I tried to mix one SQL code window and two VB.NET code windows, but posting simply crashed.
This is part of bigger app, but I’ve created simple example on SQL server express 2012: database (DBQueriesProbe) of only one table (dbo.LookUP) and two functions (GetIDFromName and NameExist).
I populated the table with: Item01, Item02, Item03.
Then, I opened VS started new winform project. I added new data source from that database selecting only my table and those functions. I built the app, opened data set designer and got my table with its table adapter and another rectangle named QueriesTableAdapter with my two functions.
CREATE TABLE [dbo].[LookUP](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_LookUP] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE FUNCTION [dbo].[GetIDFromName](@name nvarchar(50))
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar int
-- Add the T-SQL statements to compute the return value here
SELECT @ResultVar = ID from dbo.LookUP where Name=@Name
if @@ROWCOUNT=0 set @ResultVar=0
-- Return the result of the function
RETURN @ResultVar
END
CREATE FUNCTION [dbo].[NameExist](@name as nvarchar(50))
RETURNS bit
AS
BEGIN
DECLARE @ResultVar bit
Declare @SelTable as table (ID int,Name nvarchar(50))
insert into @SelTable select * from dbo.LookUP where Name=@Name
if @@ROWCOUNT>0 set @ResultVar=1 else set @ResultVar=0
RETURN @ResultVar
END
GO
I right-clicked on LookUPTableAdapter, and selected preview data. Results were expected. When I tried to do the same with functions, preview for them was not available. Not knowing any better, I added another query in Queries table adapter, creating it from stored procedure GetIDFromName, only changing its name to fGetIDFromName, because you cannot have two queries with the same name. This new query appeared in preview menu, but as function of dataset not as function of QueriesTableAdapter. This new query returned result as expected.
But now, I come to interesting part. I didn’t want to bind data to something on the form. I wanted to try those functions. So I added new module to the project (DataManagement) with this code.
Module DataManagement
Public dsQP As New DBQueriesProbeDataSet
Public taLookUP As New DBQueriesProbeDataSetTableAdapters.LookUPTableAdapter
Public taQueries As New DBQueriesProbeDataSetTableAdapters.QueriesTableAdapter
Public tamQP As New DBQueriesProbeDataSetTableAdapters.TableAdapterManager
Public bsLookUP As New BindingSource
Public Sub InitDataManagement()
tamQP.LookUPTableAdapter = taLookUP
taLookUP.Fill(dsQP.LookUP)
bsLookUP.DataSource = dsQP
bsLookUP.DataMember = dsQP.LookUP.TableName
End Sub
End Module
On the form I just added one multiline text box to show the results. In form load event I added this code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitDataManagement()
Dim sMess As String = ""
bsLookUP.MoveFirst()
Dim CurLU = CType(CType(bsLookUP.Current, DataRowView).Row, DBQueriesProbeDataSet.LookUPRow)
sMess = CurLU.Name & vbCrLf
sMess &= CInt(taQueries.GetIDFromName(CurLU.Name)).ToString & vbCrLf
sMess &= CInt(taQueries.fGetIDFromName(CurLU.Name)).ToString & vbCrLf
sMess &= CBool(taQueries.NameExist(CurLU.Name)).ToString
TextBox1.Text = sMess
End Sub
End Class
And got this as a result
Item01
1
0
True
And this shows that functions from QueriesTableAdapter added by dataset designer worked, but the one added manually didn’t work, even though it was created from the same “stored” function.
Can anyone explain this?
Continue reading...