ASP.NET Accordion control with SQL Server Connectivity

The Accordion is a web control that allows you to provide multiple panes and display them one at a time. It is like having several CollapsiblePanels where only one can be expanded at a time.

How to populate data from the SQL SERVER database and integrate with Accordion Control

 

I tried from the internet but hardly found something good which is so friendly.


I decided to make a code for this.
 

First create tables in the database 


CREATE
TABLE [dbo].[Category](

      [CategoryId] [int] IDENTITY(1,1) NOT NULL,

      [CatName] [nvarchar](50) NULL,

      [CatDesc] [nvarchar](255) NULL

)

 

CREATE TABLE [dbo].[SubCategory](

      [SubCategoryID] [int] IDENTITY(1,1) NOT NULL,

      [CategoryID] [int] NOT NULL,

      [SubCategoryName] [varchar](30) NULL

)

Inserting values for Main Category Table

 

Insert Into Category (CatName,CatDesc) Values (‘Hospital’,‘Hospital’)

Go

Insert Into Category (CatName,CatDesc) Values (‘School’,‘School’)

Go

Insert Into Category (CatName,CatDesc) Values (‘Hotel’,‘Hotel’)

 

Select * from Category

 

Result: 

 Category

 

Some values for Sub Category Table

 

Insert Into SubCategory (CategoryID,SubCategoryName) Values (1,‘Hospital 1′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (1,‘Hospital 2′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (1,‘Hospital 3′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (2,‘School 1′)

Go

Insert Into SubCategory (CategoryID,SubCategoryName) Values (2,‘School 2′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (3,‘Hotel 1′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (3,‘Hotel 2′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (3,‘Hotel 3′)

Go
Insert Into SubCategory (CategoryID,SubCategoryName) Values (3,‘Hotel 4′)

Go

Select * from SubCategory

 

Result:

Sub Cateogry

 

Now we have two tables, let’s create a stored procedure.

 

CREATE PROCEDURE [dbo].[PROC_CATEGORY_GETALL]    

AS      

BEGIN 

 SELECT      

     DISTINCT CATEGORY.CATEGORYID,

     ISNULL(CATEGORY.CATNAME,) AS CATNAME     

 FROM

    CATEGORY

      

 SELECT      

      CATEGORY.CATEGORYID,
     
ISNULL(CATEGORY.CATNAME,) AS CATNAME,     

      ISNULL(SUBCATEGORY.SUBCATEGORYNAME,) AS SUBNAME

 FROM

      CATEGORY INNER JOIN SUBCATEGORY

      ON CATEGORY.CategoryID=SubCategory.CategoryID     

 ORDER BY CATEGORY.CATNAME ASC     

END 

 

GO

 

In the above stored procedure I am returning two tables, Main and Sub Category both, So I don’t need to go and touch database for each Category.

I don’t want for each Category it will go back and forth toward server.

 

Create any Webpage in your AJAX Enabled web application

 

In a Webpage ASPX write below code

 

<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”98%”>

  <tr>

      <td>

       <cc1:Accordion ID=”MyAccordion” runat=”Server” SelectedIndex=”0″ HeaderCssClass=”accordionHeader”HeaderSelectedCssClass=”accordionHeaderSelected” ContentCssClass=”accordionContent”AutoSize=”None” FadeTransitions=”true” TransitionDuration=”250″ FramesPerSecond=”40″ 

RequireOpenedPane=”false” SuppressHeaderPostbacks=”true”>

            </cc1:Accordion>

          </td>

        </tr>

 </table>

 

Now CODE Behind.


Private
Sub PopulateGrid()


   Dim sqlConn As New SqlConnection

   Dim sqlCmd As New SqlCommand(“PROC_CATEGORY_GETALL”, sqlConn)

 

   Dim DA As New SqlDataAdapter

   Dim ds As New DataSet

 

   Dim intRow As Integer

   Dim intRowPenal As Integer

 

   Dim acpPane As AjaxControlToolkit.AccordionPane

   Dim lblHeader As Label

 

   sqlConn = “Opened Connection”e.g.”openConnection()returns me a connection object”

   sqlCmd.Connection = sqlConn

   sqlCmd.CommandType = CommandType.StoredProcedure

   DA.SelectCommand = sqlCmd

   DA.Fill(ds)


  For intRow = 0 To ds.Tables(0).Rows.Count – 1

       lblHeader = New Label

       lblHeader.Text = ds.Tables(0).Rows(intRow)(“CatName”)

 

       acpPane = New AjaxControlToolkit.AccordionPane

       acpPane.HeaderContainer.Controls.Add(lblHeader)


      
Dim dv As DataView = ds.Tables(1).DefaultView
      
dv.RowFilter = “CATEGORYID=’” + ds.Tables(0).Rows(intRow)(“CATEGORYID”).ToString + “‘”

       Dim lblContent As New Label

       For intRowPenal = 0 To dv.Count – 1

            lblContent.Text = lblContent.Text & “<div style=’padding-top: 5px; padding-left: 10px;’><a href=’” & ResolveUrl(dv.Item(intRowPenal)(“SUBCATEGORYNAME”)) & “‘>” + dv.Item(intRowPenal)(“SUBNAME”) + “</a>” & “</div>”

      Next

      acpPane.ContentContainer.Controls.Add(lblContent)

      MyAccordion.Panes.Add(acpPane)

    Next

 

    DA.Dispose()

    DA = Nothing

    sqlCmd.Dispose()

    sqlCmd.Connection.Close()

    sqlCmd = Nothing

    sqlConn.Close()

    sqlConn = Nothing


  End Sub

 

Note : Change Sub Category Hyperlink as per your wish.

Leave your message if it solve your problem.

Joggee

kick it on DotNetKicks.com

Ajax, ASp.NET 2005, Database Programming , , ,

Microsoft Tech·Ed North America 2008 Developers: Conference Registration

Guys, Microsoft helping once again and give more exposure to the developers Below is something is which is going to happen shortly.

June 3-6, 2008

Don’t miss your opportunity to attend the Bill Gates keynote. Register now to learn about Gates’ vision for the future of the IT industry.

Last chance discount: Save $200.
Register by Friday, May 2, 2008, to receive the discounted rate of just US$1,795—that’s US$200 off the regular rate of US$1,995!

Attendee registration
In addition to registering for the conference, you must make hotel reservations during the registration process in order to receive special discounted conference room rates from select hotels. View the list of conference hotels for room rates, and check out the hotel location map.

for more detail visit here: http://www.microsoft.com/events/teched2008/developer/registration/regprocess.mspx

Conference, Conferences , ,

ResolveURL Method Part 2

How to access ResolveURL Method at code behind or any independent class. for example
CommonFunction.vb

Include Namespace “Imports System.Web”

Where you wanted to call this funcation in your code.

Dim instance As New Control
Dim returnValue As String
returnValue = instance.ResolveUrl(“~/Joggee/Default.aspx“)
 

 

“<a href=”  & returnValue & “ title=”Joggee”>” & Joggee & “</a>”

ASP.NET Tips

SQL SERVER (T-SQL)- PATINDEX

In Sql Server PatIndex is really useful method works like SPLIT funcation in .net, It returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.

Sometimes you received data in a concated form and you need to seperated on the fly.
I will not go in more detail, here is the example will compelety describe how important PatIndex is.

Create table #temp
(
ZipCode Varchar(255),
Region Varchar(255))

Insert into #Temp values(’00000-00399′,’9′)
Insert into #Temp values(’00400-00599′,’8′)
Insert into #Temp values(’00600-00999′,’1′)

Select  * from #Temp
Select  Left(ZipCode,patindex(‘%-%’,ZipCode)-1) as ZipCodeFrom ,
   Right(ZipCode,patindex(‘%-%’,ZipCode)-1) as ZipCodeTo,
   Region
From
     #Temp 

 PatIndex

SQL Server 2005, SQL Tips and Tricks , ,

Visual Studio 2005 , Web Project Deployment, Website DLL

In Visual Studio 2005, they make it so simple to deploy a web site at hosting or any server.
In Visuaa Studio 2003, You need to create your different project and then take .DLL and .ASPX files CSS except code behind files. Look tiring work.
Microsoft work wonderful and make a executable file and in few steps you can deploy your website.

Here is the link to download
http://msdn.microsoft.com/en-us/asp.net/aa336619.aspx

1.Download it and Install.
2.Open your visual studio if it is open already just restart it.
3.Right click on your web project solution.
4.Choose “Add Web Development Project”
5.Specify name and location for your web project.
6.It will be automatically added to your project.
7.Right click on it and choose property pages and do your required configuration if required otherwise default also work.
8.Choose Release from debug mode.
9.Right click on the webproject again and Build.
10.You are done. Your project is done and ready to deply. It will automatically add .aspx, css and all required files and folder for the project EXCEPT CODE “BEHIND SOURCE CODE”.


ASp.NET 2005, ASP.NET Tips, Visual Studio ASP.NET , ,

2nd International Networking and Communications Conference 2008

INCC 2008
2nd International Networking and Communications Conference
May 1-4, 2008, Lahore University of Management Sciences
Lahore, Pakistan for registration and more detail
http://suraj.lums.edu.pk/incc2008


Conference, Networking, Workshop , ,

Assigning Session to nothing or Session.Abandon

I have seen so many developers once they wanted to destroy session, they used

Session(”A”) = Nothing

Its not a good practice to assign nothing object.

Correct Way:

Session.Abandon

The Session.Abandon method basically destroys a user session.

One thing I would add the current Session object will not delete untill the script on the current page have been processed completely.

Best Way:

FormsAuthentication.SignOut()
Session.Abandon()

Use Forms Authentication.Signout which will removes the forms-authentication ticket from the browser.

ASp.NET 2005, ASP.NET Tips , , ,

TODO Comments

Follow these images one by one that will guide properly how to do this.

This article related to todo comments which I have already wrote long time back
Here are the steps which will help to achieve this. for more detail please check here my

old post

Write some comments and then select option as comments in the task list.
You can also choose your keyword with the different priority level.
Task list can be created same way comments created.

ASp.NET 2005, ASP.NET Tips, Visual Studio ASP.NET , ,

ResolveUrl Method

ASP.NET’s Control class which provide a wonderful method ResolveURL thats parse relative URL’s path and make it ready and fix URL into one that is readable and usable on the requesting client.
Using tilt sign ~ that makes control execute and search inside the application.
E.g.

wwwroot/images/joggee.jpg
reference path : “~/images/joggee.jpg”


Example:
 

 

<img alt=”" height=”7″ hspace=”6″ src=<%=ResolveURL(“~/images/index_40.jpg”) %> width=”4″ />


ASp.NET 2005, Visual Studio ASP.NET ,

Exporting Data from MS SQL SERVER to MySQL

Readers, always ask me to write on CURSOR, with their customize problem, But I normally write which will help
every one. One of reader asked me long back about the cursor and exporting data from MS SQL Server to MySQL.I had the same scenario, I need to export data from MS SQL to MySQL, and I have to do it quickly.
I didnt find on internet a better solution because of the time constraint.
I have acheived my task by using CURSOR and generate customize queries, So I can execute them at MySQL database and populate required data.

My main focus is to share how to use cursor and also one of the way to export data from MS SQL Server to MySQL. 

You can customize and replace your queries with one written below.

Lets Start. 

–insert records in a temp table

Select
username
,
‘e50b13c57937586c4c6c6fa30f0bce24′ as ‘password’,
’33VPTHTe’ as ‘salt’,
‘dkRZvhwOMrXLwL8mSJA2PneuMkBCXD9aRLuWjgIF2grtYNW6bQ’ as ‘loginkey’,
user_email as ‘email’,
’1206339144′ as ‘regdate’,
’1206423077′ as ‘lastvisit’,
’0′ as ‘lastpost’,
snull(User_website,) as ‘website’,
isnull(user_icq,) as ‘icq’,
isnull(user_aim,) as ‘aim’,
isnull(user_yim,) as ‘yahoo’,
isnull(user_msnm,) as ‘msn’,
isnull(user_sig,) as ‘signature’,
isnull(user_allow_pm,’0′) as ‘receivepms’,
isnull(user_notify_pm,’0′) as ‘pmnotify’,
isnull(user_allowavatar,’0′) as ‘showavatars’,
’2′ as ‘usergroup’
into #temp
Fromphpbb_users
Where user_session_page<>0 

–Declaring variables to insert column values later
Declare @username varchar(1000)
Declare @password varchar(500)
Declare @salt varchar (250)
Declare @loginkey varchar(500)
Declare @email varchar(500)
Declare @regdate varchar(500)
Declare @lastvisit varchar(500)
Declare @lastpost varchar(500)
Declare @website varchar(500)
Declare @icq varchar(255)
Declare @aim varchar(255)
Declare @yahoo varchar(255)
Declare @msn varchar(255)
Declare @signature varchar(255)
Declare @receivepms varchar(255)
Declare @pmnotify varchar(255)
Declare @showavatars varchar(255)
Declare @usergroup varchar(10)

–declaration of the cursor
DECLARE @getBB CURSOR

–Initializing of cursor for particular query result.
SET @getBB = CURSOR FOR SELECT * FROM #TEMP

OPEN @getBB –opening a cursor
FETCH NEXT –Syntax of cursor to move next
FROM @getBB INTO – Inserting single row data as a column to variables

@username ,@password,@salt,@loginkey ,@email,@regdate,@lastvisit,@lastpost,
@website,@icq,@aim ,@yahoo,@msn,@signature,@receivepms,@pmnotify,@showavatars ,@usergroup

WHILE @@FETCH_STATUS = 0 – condition execute until record end

BEGIN 

– Here you can write your customize result or further execution.

Print ‘ INSERT INTO users(username, password, salt,loginkey,email, regdate, lastvisit,
lastpost, website, icq, aim, yahoo, msn, signature, receivepms,
pmnotify,showavatars,usergroup
)VALUES

(

”’

+ @username + ”’,”’+ @password + ”’,”’+ @salt + ”’,
”’
+ @loginkey + ”’,”’+ @email + ”’,”’+ @regdate + ”’,
”’
+ @lastvisit + ”’,’+ @lastpost + ‘,”’+ @website + ”’,
”’
+ @icq + ”’,”’+ @aim + ”’,”’+ @yahoo + ”’,”’+ @msn + ”’,
”’
+ @signature + ”’,”’+ @receivepms + ”’,”’+ @pmnotify + ”’,
”’
+ @showavatars +”’,”’ + @usergroup + ”’);’

FETCH NEXT – Moving next
FROM @getBB INTO – Insert next record to the variables.
@username ,@password,@salt,@loginkey,
@email,@regdate,@lastvisit,@lastpost,@website,@icq,
@aim ,@yahoo,@msn,@signature,@receivepms,@pmnotify,@showavatars ,@usergroup
ENDCLOSE @getBB – Closing variable
DEALLOCATE @getBB – removing from the memory
GO–droping table.

DROP TABLE #TEMP
—————————————————————————————–
Hope this will solve Export data from MS Sql Server to MySQL and also explain a cursor.

Rana

MySQL, SQL Server 2005, SQL Server 2008, SQL Sever, SQL Tips and Tricks , , ,