maqskywalker
asked on
saving variables into comma separated strings in visual basic
I'm using ASP.NET Web Forms and VB.
I'm using Sql Server.
I'm using the DataGrid control. Yes I know the newer grid is GridView, but I need to do this with DataGrid.
I'm using the Employees table from the Northwind database.
Here is the script to create the table:
Here is the script to create the stored procedure that calls the table:
This is my existing working code:
NWEmployees1.aspx
NWEmployees1.aspx.vb
So on the VB code behind for the page I call the stored procedure and then bind it to the DataGrid.
So when I run my page above it looks like this:
Now if I check some check boxes the id of the rows I checked are then displayed on a label like this after I click the button:
If you look at this function View_Selected in the vb code i'm looping through all the DataGridItems where the checkbox was checked and displaying the id in a label.
How do I revise my example to store my checkbox selections into a string variable called TestString1 separated by a comma and display that string in a label ?
So for example If I click in the check box for EmployeeID 1 , 5, and 8
then the value stored in the string variable called TestString1 would be "1,5,8"
So then the label on the screen would display this: 1,5,8
I'm using Sql Server.
I'm using the DataGrid control. Yes I know the newer grid is GridView, but I need to do this with DataGrid.
I'm using the Employees table from the Northwind database.
Here is the script to create the table:
USE [Northwind]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employees](
[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](20) NOT NULL,
[FirstName] [nvarchar](10) NOT NULL,
[Title] [nvarchar](30) NULL,
[TitleOfCourtesy] [nvarchar](25) NULL,
[BirthDate] [datetime] NULL,
[HireDate] [datetime] NULL,
[Address] [nvarchar](60) NULL,
[City] [nvarchar](15) NULL,
[Region] [nvarchar](15) NULL,
[PostalCode] [nvarchar](10) NULL,
[Country] [nvarchar](15) NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[EmployeeID] 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
SET IDENTITY_INSERT [dbo].[Employees] ON
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (1, N'Davolio', N'Nancy', N'Sales Representative', N'Ms.', CAST(N'1948-12-08T00:00:00.000' AS DateTime), CAST(N'1992-05-01T00:00:00.000' AS DateTime), N'507 - 20th Ave. E.
Apt. 2A', N'Seattle', N'WA', N'98122', N'USA')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (2, N'Fuller', N'Andrew', N'Vice President, Sales', N'Dr.', CAST(N'1952-02-19T00:00:00.000' AS DateTime), CAST(N'1992-08-14T00:00:00.000' AS DateTime), N'908 W. Capital Way', N'Tacoma', N'WA', N'98401', N'USA')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (3, N'Leverling', N'Janet', N'Sales Representative', N'Ms.', CAST(N'1963-08-30T00:00:00.000' AS DateTime), CAST(N'1992-04-01T00:00:00.000' AS DateTime), N'722 Moss Bay Blvd.', N'Kirkland', N'WA', N'98033', N'USA')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (4, N'Peacock', N'Margaret', N'Sales Representative', N'Mrs.', CAST(N'1937-09-19T00:00:00.000' AS DateTime), CAST(N'1993-05-03T00:00:00.000' AS DateTime), N'4110 Old Redmond Rd.', N'Redmond', N'WA', N'98052', N'USA')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (5, N'Buchanan', N'Steven', N'Sales Manager', N'Mr.', CAST(N'1955-03-04T00:00:00.000' AS DateTime), CAST(N'1993-10-17T00:00:00.000' AS DateTime), N'14 Garrett Hill', N'London', NULL, N'SW1 8JR', N'UK')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (6, N'Suyama', N'Michael', N'Sales Representative', N'Mr.', CAST(N'1963-07-02T00:00:00.000' AS DateTime), CAST(N'1993-10-17T00:00:00.000' AS DateTime), N'Coventry House
Miner Rd.', N'London', NULL, N'EC2 7JR', N'UK')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (7, N'King', N'Robert', N'Sales Representative', N'Mr.', CAST(N'1960-05-29T00:00:00.000' AS DateTime), CAST(N'1994-01-02T00:00:00.000' AS DateTime), N'Edgeham Hollow
Winchester Way', N'London', NULL, N'RG1 9SP', N'UK')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (8, N'Callahan', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09T00:00:00.000' AS DateTime), CAST(N'1994-03-05T00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', N'98105', N'USA')
INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (9, N'Dodsworth', N'Anne', N'Sales Representative', N'Ms.', CAST(N'1966-01-27T00:00:00.000' AS DateTime), CAST(N'1994-11-15T00:00:00.000' AS DateTime), N'7 Houndstooth Rd.', N'London', NULL, N'WG2 7LT', N'UK')
SET IDENTITY_INSERT [dbo].[Employees] OFF
Here is the script to create the stored procedure that calls the table:
USE [Northwind]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployees]
AS
SELECT [EmployeeID]
,[LastName]
,[FirstName]
,[Title]
,[BirthDate]
,[HireDate]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[Country]
FROM [Northwind].[dbo].[Employees]
GO
This is my existing working code:
NWEmployees1.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="NWEmployees1.aspx.vb" Inherits="NWEmployees1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.DGHeaderText {font-family: Arial; font-size: 12px; color: #000000; font-weight: bold;}
.DGBodyText {font-family: Arial; font-size: 12px; color: #000000; font-weight: normal;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="DivEmployees">
<asp:DataGrid id="DataGridEmployees" runat="server" AutoGenerateColumns="False" Font-Size="8pt" CellPadding="3" BorderColor="Black" Width="400px" CssClass="DGHeaderText">
<Columns>
<asp:TemplateColumn HeaderText="contract">
<HeaderTemplate>
<input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" runat="server" id="EmpId" onclick="CheckChanged();" checked='false' name="EmpId" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="EmployeeID">
<ItemTemplate>
<asp:Label id="EmployeeID" class="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeID") %>' runat="server" />
</ItemTemplate>
<%--<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />--%>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="FirstName">
<ItemTemplate>
<asp:Label id="FirstName" class="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="LastName">
<ItemTemplate>
<asp:Label id="LastName" class="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Title">
<ItemTemplate>
<asp:Label id="Title" class="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<%--<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />--%>
<HeaderStyle HorizontalAlign="Center" />
</asp:DataGrid> <br/>
<asp:button id="selBtn" onclick="View_Selected" runat="server" name="selBtn" text="Show Selection"></asp:button>
</div>
</form>
</body>
</html>
NWEmployees1.aspx.vb
Imports System
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Partial Class NWEmployees1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bindGrid()
End If
End Sub
Private Sub bindGrid()
Dim mySqlConnection As SqlConnection = New SqlConnection("Data Source=PSW-MQUEZADA\SQLEXPRESS;Initial Catalog=TestDatabase3;Integrated Security=True;")
Dim mySqlCommand As SqlCommand = mySqlConnection.CreateCommand()
mySqlCommand.CommandText = "GetEmployees"
mySqlCommand.CommandType = CommandType.StoredProcedure
Dim mySqlDataAdapter As SqlDataAdapter = New SqlDataAdapter()
mySqlDataAdapter.SelectCommand = mySqlCommand
Dim ds As DataSet = New DataSet()
mySqlConnection.Open()
mySqlDataAdapter.Fill(ds, "Employees")
DataGridEmployees.DataSource = ds.Tables("Employees").DefaultView
DataGridEmployees.DataBind()
End Sub
Public Sub View_Selected(ByVal sender As Object, ByVal e As EventArgs)
' ----- loop through checked DataGrid items -----
For Each di As DataGridItem In DataGridEmployees.Items
Dim chkBx As HtmlInputCheckBox = CType(di.FindControl("EmpId"), HtmlInputCheckBox)
If chkBx IsNot Nothing AndAlso chkBx.Checked Then
Dim lbl As Label = CType(di.FindControl("EmployeeID"), Label)
Response.Write(lbl.Text & "<br>")
End If
Next
' -----------------------------------------------
End Sub
End Class
So on the VB code behind for the page I call the stored procedure and then bind it to the DataGrid.
So when I run my page above it looks like this:
Now if I check some check boxes the id of the rows I checked are then displayed on a label like this after I click the button:
If you look at this function View_Selected in the vb code i'm looping through all the DataGridItems where the checkbox was checked and displaying the id in a label.
How do I revise my example to store my checkbox selections into a string variable called TestString1 separated by a comma and display that string in a label ?
So for example If I click in the check box for EmployeeID 1 , 5, and 8
then the value stored in the string variable called TestString1 would be "1,5,8"
So then the label on the screen would display this: 1,5,8
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are welcome!
ASKER