473,791 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parameters

Hi,

My purpose consists on filtering the fields "years" and "clients" of a
project WinForm, in a similar way to as I make it

without any problems in a previous project ASP.NET.

The filtering task must be on the server side because my database is very
extensive and, in this case, filter with Dataview

is not convenient.

I have a database in SQL Server side with a view, two functions and a stored
procedure:

1. The table "tblSales" has three fields: year, client and quantity.

2. The function "fn_com1" is prepared to filter the field "year".

3. The function "fn_com2" has as purpose filtering of the field "client."

4. The stored procedure "sp_fncom" is created starting from the function
"fn_com2" and it contains the year, client and

quantity fields.

5. The SQL Server has a denominated function "CSVToTable ", a Comma Separates
Valued (CSV) that works fine.

II-WinForm1:

Visual Studio WinForm, Form1, Design Mode:

Controls:

ListBox1: It contains as Items the field "year"
("2000","2001", "2002","2003"," 2004","2005").

ListBox2: It contains as Items the field "client". Must be (" 101"," 102","
103")

(Important: Both Listbox has the multiselection activated as Multiextended.)

Button1: To execute the stored procedure already described.

Datagrid1: To show results of the stored procedure.

Also, the corresponding accesses to data, etc. (SqlDataAdpater , Datasets, etc)

III- WinForm1 Visual Basic code:

Here I have the greatest problem. I don't find the correct syntax to execute
the queries.

I don’t find the necessary Visual Studio Space Name. Maybe I need to create
my custom Space Name?

I need inside the Windows form code something similar to:
Sub ChangeWhereClau se (ByVal Sender As System.Object, ByVal e As
System.EvenArgs )

'For example, to filter "year" field I put:

Protected li As System.Web.UI.W ebControls.List Item.

(Used in a previous project ASP.NET and ListItem class not exists in Windows
Forms).
Dim strOrderNumbers As String = ""
For Each li In listbox1.Items
If li.Selected Then
strOrderNumbers &= li.Value & ","
End If
Next
If strOrderNumbers .Length > 0 Then
strOrderNumbers = Left(strOrderNu mbers, _
strOrderNumbers .Length() - 1)
End If

....etc

End sub
---------------------------------------------------------------------------------

In the followings lines I show the Public Class Form1 code:
Imports ADODB
Imports System
Imports System.Collecti ons
Imports System.Data
Imports System.Data.Sql Client
Imports System.io
Imports System.Configur ation
Imports Microsoft.Visua lBasic
Public Class Form1
Inherits System.Windows. Forms.Form
#Region " Código generado por el Diseñador de Windows Forms "

Public Sub New()
MyBase.New()

'El Diseñador de Windows Forms requiere esta llamada.
InitializeCompo nent()

'Agregar cualquier inicialización después de la llamada a
InitializeCompo nent()

End Sub

'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Requerido por el Diseñador de Windows Forms
Private components As System.Componen tModel.IContain er

'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
'Puede modificarse utilizando el Diseñador de Windows Forms.
'No lo modifique con el editor de código.

Friend WithEvents ListBox1 As System.Windows. Forms.ListBox
Friend WithEvents SqlDataAdapter1 As System.Data.Sql Client.SqlDataA dapter
Friend WithEvents SqlSelectComman d1 As System.Data.Sql Client.SqlComma nd
Friend WithEvents SqlConnection1 As System.Data.Sql Client.SqlConne ction
Friend WithEvents DataSet11 As Experimental.Da taSet1
Friend WithEvents DataGrid1 As System.Windows. Forms.DataGrid
Friend WithEvents ListBox2 As System.Windows. Forms.ListBox
Friend WithEvents Button1 As System.Windows. Forms.Button
Protected li As System.Web.UI.W ebControls.List Item ‘ Not works in WinForms
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.ListBox1 = New System.Windows. Forms.ListBox
Me.SqlDataAdapt er1 = New System.Data.Sql Client.SqlDataA dapter
Me.SqlSelectCom mand1 = New System.Data.Sql Client.SqlComma nd
Me.SqlConnectio n1 = New System.Data.Sql Client.SqlConne ction
Me.DataSet11 = New Experimental.Da taSet1
Me.DataGrid1 = New System.Windows. Forms.DataGrid
Me.ListBox2 = New System.Windows. Forms.ListBox
Me.Button1 = New System.Windows. Forms.Button
CType(Me.DataSe t11,
System.Componen tModel.ISupport Initialize).Beg inInit()
CType(Me.DataGr id1,
System.Componen tModel.ISupport Initialize).Beg inInit()
Me.SuspendLayou t()
'
'ListBox1
'
Me.ListBox1.Ite ms.AddRange(New Object() {"2000", "2001", "2002",
"2003"})
Me.ListBox1.Loc ation = New System.Drawing. Point(120, 40)
Me.ListBox1.Nam e = "ListBox1"
Me.ListBox1.Sel ectionMode =
System.Windows. Forms.Selection Mode.MultiExten ded
Me.ListBox1.Siz e = New System.Drawing. Size(120, 95)
Me.ListBox1.Tab Index = 0
'
'SqlDataAdapter 1
'
Me.SqlDataAdapt er1.SelectComma nd = Me.SqlSelectCom mand1
Me.SqlDataAdapt er1.TableMappin gs.AddRange(New
System.Data.Com mon.DataTableMa pping()
{New System.Data.Com mon.DataTableMa pping("Table", "spFnCom", New
System.Data.Com mon.DataColumnM apping() {New
System.Data.Com mon.DataColumnM apping("year",
"year"), New System.Data.Com mon.DataColumnM apping("client" , "client"), New
System.Data.Com mon.DataColumnM apping("quantit y", " quantity ")})})
'
'SqlSelectComma nd1
'
Me.SqlSelectCom mand1.CommandTe xt = "[ spFnCom]"
Me.SqlSelectCom mand1.CommandTy pe =
System.Data.Com mandType.Stored Procedure
Me.SqlSelectCom mand1.Connectio n = Me.SqlConnectio n1
Me.SqlSelectCom mand1.Parameter s.Add(New
System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
System.Data.Sql DbType.Int, 4,
System.Data.Par ameterDirection .ReturnValue, False, CType(0, Byte), CType(0,
Byte), "",
System.Data.Dat aRowVersion.Cur rent, Nothing))
Me.SqlSelectCom mand1.Parameter s.Add(New
System.Data.Sql Client.SqlParam eter("@year", System.Data.Sql DbType.NVarChar ,
2000))
Me.SqlSelectCom mand1.Parameter s.Add(New
System.Data.Sql Client.SqlParam eter("@client",
System.Data.Sql DbType.NVarChar , 2000))
'
'SqlConnection1
'
Me.SqlConnectio n1.ConnectionSt ring = "workstatio n id=RENEPC;packe t
size=4096;integ rated security=SSPI;d ata source=""RE" & _
"NEPC"";per sist security info=False;init ial catalog=prueba"
'
'DataSet11
'
Me.DataSet11.Da taSetName = "DataSet1"
Me.DataSet11.Lo cale = New System.Globaliz ation.CultureIn fo("es-ES")
'
'DataGrid1
'
Me.DataGrid1.Da taMember = ""
Me.DataGrid1.Da taSource = Me.DataSet11. spFnCom
Me.DataGrid1.He aderForeColor = System.Drawing. SystemColors.Co ntrolText
Me.DataGrid1.Lo cation = New System.Drawing. Point(176, 184)
Me.DataGrid1.Na me = "DataGrid1"
Me.DataGrid1.Si ze = New System.Drawing. Size(296, 80)
Me.DataGrid1.Ta bIndex = 1
'
'ListBox2
'
Me.ListBox2.Ite ms.AddRange(New Object() {"101", "102", "103"})
Me.ListBox2.Loc ation = New System.Drawing. Point(296, 40)
Me.ListBox2.Nam e = "ListBox2"
Me.ListBox2.Sel ectionMode =
System.Windows. Forms.Selection Mode.MultiExten ded
Me.ListBox2.Siz e = New System.Drawing. Size(120, 95)
Me.ListBox2.Tab Index = 2
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(536, 80)
Me.Button1.Name = "Button1"
Me.Button1.TabI ndex = 3
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(664, 406)
Me.Controls.Add (Me.Button1)
Me.Controls.Add (Me.ListBox2)
Me.Controls.Add (Me.DataGrid1)
Me.Controls.Add (Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataSe t11,
System.Componen tModel.ISupport Initialize).End Init()
CType(Me.DataGr id1,
System.Componen tModel.ISupport Initialize).End Init()
Me.ResumeLayout (False)

End Sub

#End Region
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s)
Handles Button1.Click
Dim sOrder As String = ""

For Each oList In ListBox1.Items

If oList.Selected Then

sOrder &= oList.Value & ","

End If
Next

If sOrder.Length > 0 Then

sOrder = Strings.Left(sO rder, sOrder.Length() - 1)

End If

'----------------------------------------------------------
Dim sOrder1 As String = ""
For Each oList In ListBox2.Items

If oList.Selected Then

sOrder1 &= oList.Value & ","

End If
Next

If sOrder1.Length > 0 Then

sOrder1 = Strings.Left(sO rder1, sOrder1.Length( ) - 1)

End If

With Me.SqlSelectCom mand1
.Parameters("@y ear").Value = sOrder
.Parameters("@c lient").Value = sOrder1
Me.SqlDataAdapt er1.Fill (DataSet11. spFnCom)
End With

End Sub

End Class

--------------------------------------------------------------------
IV- SQL Server Scripts:

SET QUOTED_IDENTIFI ER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER FUNCTION CSVToTable (@CSVList varchar(8000))
RETURNS @csvtable table (val varchar(1000))
AS
BEGIN
-- variables for position marking
declare @separatorposit ion int, @arrayvalue varchar(1000)
-- Pad the list if needed
if substring(rtrim (@csvlist),len( rtrim(@csvlist) ),1)<>','
set @csvlist = @csvlist + ','
-- Loop through string
while patindex('%,%', @csvlist) <> 0
begin
select @separatorPosit ion = patindex('%,%', @csvlist)
select @arrayValue = left(@csvlist, @separatorPosit ion - 1)
INSERT into @csvtable(val) values (rtrim(ltrim(@a rrayValue)))

select @csvlist = stuff(@csvlist, 1,@separatorPos ition, '')
end
-- return table
return
END
GO
SET QUOTED_IDENTIFI ER OFF
GO
SET ANSI_NULLS ON
GO
------------------------------------------------------------------
create view dbo.vs_Sales

as

select year, client, quantity

from dbo.tbl_Sales

go

-------------------------------------------------------------------

Create function dbo.fncom1

(@year nvarchar (2000))

returns table

as

return (select year, client, quantity

from dbo.vs_Sales

where year in (select val from dbo.csvtotable (@year)))

go

-------------------------------------------------------------------

create function dbo.fncom2

(@year nvarchar (2000), @client nvarchar (2000))

returns table

as

return (select year, client, quantity

from dbo. Fncom1 (@year)

where client in (select val from dbo.csvtotable (@client)))

go

---------------------------------------------------------------------

create procedure dbo.spfncom

(@year (2000), @client(2000))

as

select top 100 percent year, client, quantity

from dbo.fncompara1 (@year, @client)

go
----------------------------------------------------------------------

Thank you in advance,
Adis
Nov 19 '05 #1
0 1427

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1658
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub UploadData(ByVal sender As Object, ByVal e As EventArgs) Dim MyConn As New
0
302
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub UploadData(ByVal sender As Object, ByVal e As EventArgs) Dim MyConn As New
14
3275
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
2
2629
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying to update a record, I get a "No value given for one or more parameters." error message. I use a Select with parameters and an Update with parameters. The select works fine. I thought I've tried everything (evidently not) to get this working. ...
12
2675
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be avoided. I'd like to hear your various opinions on this matter.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.