Wednesday, May 2, 2012

Update da una Select

Se ci fosse bisogno di fare un update su diversi campi copiandoli da un'altra tabella, potete usare questo script:
update
 A_Categorie
set
 A_Categorie.Descrizione_DE = B_Categorie.Descrizione_DE,
 A_Categorie.descrizione_singolare_DE = B_Categorie.descrizione_singolare_DE
from
 A_Categorie
inner join
 B_Categorie
on
 A_Categorie.idCategoria = B_Categorie.idCategoria

Nell'esempio, copio i campi "Descrizione_DE" (ecc..) dalla tabella "B_Categorie" alla tabella "A_Categorie".

Tuesday, May 24, 2011

Concatenare campi di diversi records (senza cursore)

Se avete la necessita' di concatenare in una stringa dei campi che sono su piu' records, normalmente viene suggerito l'uso dei cursori che eseguono un ciclo sulle righe interessate dalla clausola "where".
Per fortuna esite un modo piu' veloce per ottenere la concatenazione desiderata senza l'uso di cursori, senza stressare la cpu del server e quella del programmatore!

Come test, ho usato il db d'esempio AdventureWorks e la tabella "Production.Product". Voglio quindi concatenare e separare con una virgola, tutti i prodotti il cui colore e' "Black".


Per per poter concaternare i prodotti, ho usato il seguente codice:

SELECT STUFF ((SELECT ', ' + [Name]
FROM Production.Product WHERE Color = 'Black'
FOR XML PATH ('')),1,2,'') AS Black_Products

Usando quindi,
FOR XML PATH
ma anche la funzione (serve per separare con le virgole al meglio)
STUFF
potrete risolvere velocemente e senza cursori, la vostra concatenazione!

Maggiori info sulla funzione STUFF le trovate qui, per quanto riguarda le istruzioni FOR XML PATH, potete guardare qui.

Buon lavoro!

Sunday, April 3, 2011

Automatically create all Store Procedures

I have often to write many store procedures in SQL Server!
So i decided to speed up my work, writing just 3 Store Procedures that automatically create all "Insert", "Update", "Delete" Store Procedures, of all tables, in the selected DB.

declare crs_tab cursor for
select [name] from sys.tables
where [name] not like 'sys%'

open crs_tab

As you can see, I used the "cursor" and "information_schema.columns" to get infos about all tables (and view) from the DB.

declare crs_structure cursor for
select c.[name], data_type ,ISC.character_maximum_length,ISC.character_maximum_length
from sys.columns C
join sys.tables T on t.[object_id]=c.[object_id]
join information_schema.columns ISC
on ISC.table_name=t.[name] and Column_name=c.[name]
ISC.table_name=t.[name] and Column_name=c.[name]

Download the 3 txt and create the 3 store procedures from these files.
Run the store procedures!




Then you will have all Insert, Update and Delete Store Procedures created from all Tables of your DB!

Good work and enjoy!

p.s.
These Store Procedures, are only a starting point... so sometimes, if your tables are not "standard", you have to create them by hand or customize the 3 txt :)

Friday, February 4, 2011

Blogger!

Google ha appena implementato Blogger per Android, versione ufficiale.
Era ora!!! (post scritto da Android ovviamente!)

Tuesday, August 3, 2010

Sql Server 2008 R2

Se si usa Sql Server 2008 R2, e si prova a modificare una tabella (progetta), si scoprira' che Management Studio impedisce il salvataggio delle modifiche.


Sembra che questo sia il comportamento di default...
Per poter modificare le tabelle o altri oggetti basta andare in "Strumenti" e poi "Opzioni" e quindi in "Designers" e levare questa fastidiosa opzione!

Sunday, June 27, 2010

Esame Microsoft 70-528


Sunday, March 7, 2010

Esame Microsoft 70-528

Di ritorno da Mosca, mi ritrovo un pacco appena arrivato dall' Inghilterra con il manuale per l' esame Microsoft .NET Framework 2.0 - Web-based Client Development.
Questi gli argomenti principali dell' esame:
creating and programming a Web application;
integrating data in a Web application by using ADO.NET, XML, and data-bound controls;
creating custom Web controls;
tracing, configuring, and deploying applications;
customizing and personalizing a Web application;
implementing authentication and authorization;
creating ASP.NET mobile Web applications;

Come per il precedente esame, mi ci vorranno forse due mesi per studiarmelo...
Spero di riuscire nell' impresa !!!

Thursday, February 25, 2010

Esame Microsoft 70-536 - Passato!!!

Oggi ho finalmente fatto l'esame Microsoft .NET Framework 2.0 - Application Development Foundation (codice esame 70-536)
Passato!!! :)
La mia preparazione e' durata due mesi, lavoro e altri impegni non hanno reso possibile un tempo inferiore...
L'esame si e' svolto in questo modo:
mi sono ovviamente recato in un centro Microsoft predisposto per gli esami;
un computer con un'applicazione appositamente configurata per l'esame in questione;
collegamento con gli USA per controllare il corretto svolgimento dell'esame;
40 domande a risposta multipla (da 4 a 6 risposte possibili);
domande in inglese;
poco piu' di 2 ore di tempo.

Devo dire che una delle maggiori difficolta' e' stata quella di studiare su un manuale in inglese...
Inoltre certi argomenti nel libro e nell'esame, erano su delle Classi del Framework 2.0 con cui non ho quasi mai lavorato, quindi non semplici da ricordare.

Per qualche giorno mi riposero', poi ricomincero' a studiare per l'esame: Microsoft .NET Framework 2.0 - Web-based Client Development (codice esame 70-528)

Se passero' pure questo, otterro' la certificazione: MCTS .NET Framework 2.0 Web Applications !!!

Wednesday, December 23, 2009

Esame Microsoft 70-536

Inizia in questi giorni di festivita' e dopo un breve (purtroppo) periodo di riposo, la mia preparazione all'esame "70-536 Microsoft .NET Framework - Application Development Foundation" !!!
Questa la descrizione ufficiale dell' esame:

"Exam 70-536 is designed to measure your knowledge of .NET development fundamentals and is not tied to a particular version of .NET. Since the exam is now applicable to both Microsoft .NET Framework 2.0 and Microsoft .NET Framework 3.5 certification tracks, we have changed the name of the exam. Formerly TS: Microsoft .NET Framework 2.0 – Application Development Foundation, Exam 70-536 is now called TS: Microsoft .NET Framework, Application Development Foundation. If you are a .NET 2.0 developer, you do not need to learn .NET 3.5 to pass Exam 70-536; conversely, if you are a .NET 3.5 developer, you do not need to review .NET 2.0 to pass the exam."

Questo e' il primo passo per ottenere la certificazione "MCTS: .NET Framework 2.0 Web Applications" MCTS
Il manuale su cui studio e' questo, circa ottocento pagine in inglese e pagato poco piu' di 30€.
Se voi lo avete gia' superato ogni consiglio sara' molto gradito :)

Saturday, September 19, 2009

Twitter Updates: 19/09/2009

Web

Webdesign

dotNet

Friday, September 4, 2009

Twitter Updates: 04/09/2009

Design

dotNet

Webdesign

Sql

Saturday, April 25, 2009

Recursive function in Asp.Net

An important programming technique is the Recursion.
Recursive is a procedure or subroutines that contains a statement that calls itself.

In this simple example I use a recursive function to clean all TextBox even those within a container (ascx control)


Private Function CleanAll(ByVal container As Control) As ArrayList

Dim txt As TextBox
For Each c As Control In container.Controls
If c.GetType.Name = "TextBox" Then
txt = c
If txt.Text <> "" Then
txt.Text = ""
allControlls.Add(c)
End If
Else
CleanAll(c) ' invokes recursively the method
'to add more controls in the control children
End If
Next
Return allControlls

End Function

There are two ascx controls nested one inside the other.
The same thing works with panels and other asp.net containers.
If you want, you can download this example here (visual basic) or here (c#).

For more info try here and here.

That's all !

Wednesday, April 1, 2009

Add Item to DropDownList

Hi!

In this post I'll explain how to add a custom-item in a DropDownList using ListItem object.

As written in MSDN:

"ListItem represents a data item in a data-bound list control"
"A ListItem control represents an individual data item within a data-bound list control"

First of all, we have to connect a Database Table with a DropDownList. (you can see how populate a DropDownList here)

Protected Sub GetContact()

Dim obj As cl_Person = Nothing
obj = New cl_Person(System.Configuration.ConfigurationManager.AppSettings("APPConnectionString"))

Me.ddlPerson.DataSource = obj.GetContact()
Me.ddlPerson.DataValueField = "ContactID"
Me.ddlPerson.DataTextField = "FullName"
Me.ddlPerson.DataBind()

End Sub

Then in the DataBound event of the DropDownList, write this:

Protected Sub ddlPerson_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPerson.DataBound

Dim myItem As New ListItem
myItem.Text = "Choose One !!!"
myItem.Value = 0
myItem.Selected = True
Me.ddlPerson.Items.Add(myItem)

End Sub

As you can see, "Choose One !!!" rapresents our custom-item.



If you want, you can download this example here.
You can download and install AdventureWorksDB.msi database here.

Please remember to change in the web.config file, the two values "Data Source=pc01;" with
your sqlserver instance name. (probably "computername" with Sql2005/2008, but if you are using SqlExpress edition your instance name is "computername\SQLExpress")

That's all !

Tuesday, March 31, 2009

Hotfix for "Design view does not update HTML"

Hello everybody!
In a previous post, I have described an update-problem with design-view and souce-view in Visual Studio 2008.

Recently Microsoft has released some hotfix that you can download here.

Enjoy!

Wednesday, March 18, 2009

Custom Filter

There are many ways to filter data in a grid, here one of them...

All you need is to create a DataSet connected with a table and a little code!
(in the example I use "AdventureWorks" as database and "Person.CountryRegion" as table)

Let's start!

As usual, we have to write an object to connect a GridView with a source like a dataset/database.


Public Function GetCountryRegion(Optional ByVal CountryRegionCode As String = "%", _
Optional ByVal Name As String = "%") As ds_CountryRegion.CountryRegionDataTable

Dim cnn As SqlConnection = Nothing
Dim cmd As SqlCommand = Nothing
Dim da As SqlDataAdapter = Nothing
Dim sqlStatement As String

Try
sqlStatement = "SELECT * FROM Person.CountryRegion where CountryRegionCode like '" & CountryRegionCode & "%' and Name like '" & Name & "%'"

cnn = New SqlConnection(Me._connectionString)
cmd = New SqlCommand(sqlStatement, cnn)
da = New SqlDataAdapter(cmd)

cnn.Open()

_ds.CountryRegion.Rows.Clear()
da.Fill(_ds, "CountryRegion")
Catch ex As Exception
'todo: code for exceptions!
Finally
GetCountryRegion = _ds.CountryRegion

If Not (cnn Is Nothing) Then
cnn.Close()
cnn.Dispose()
cnn = Nothing
End If
If Not (da Is Nothing) Then
da.Dispose()
da = Nothing
End If
If Not (cmd Is Nothing) Then
cmd.Dispose()
cmd = Nothing
End If
End Try

End Function

As you can see, I use the keyword "Optional" for the parameters and I use "%" as default value.
In T-Sql language "%" stands for "all". If there are no parameters passed, the code reads "all" CountryRegionCode records and "all" Name records.

Another interesting thing is the use of the keyword "like" and the use of "%" at the end of the parameters in the sqlStatement string ("like 'Name%'" and "like 'CountryRegionCode%'").

In this example I can catch all records with the Name that starts with the parameter passed: all records with "Name%" starts with "I%" (Italy, Ireland, Iceland...) (ok I know, it's not easy for me to explain in eng... I'm sorry!)

sqlStatement = "SELECT * FROM Person.CountryRegion where CountryRegionCode like '" & CountryRegionCode & "%' and Name like '" & Name & "%'"




When our object is ready and connected with a GridView through the "DataSource" property, we have to use the "Databind" property to associate the data with the GridView.

In the Page_Load event, there are no parameters passed to the DataTable object (in fact the two TextBox values are empty ---> "")

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
GetGwCountryRegion()
End If

End Sub

But each time that we write something in the TextBox Name or CountryRegionCode and click on "Filter", the DataTable recives parameters and use them for filtering.

That's all!

You can download this my little example here.
You can download and install AdventureWorksDB.msi database here.

Please remember to change in the web.config file, the two values "Data Source=pc01;" with
your sqlserver instance name. (probably "computername" with Sql2005/2008, but if you are using SqlExpress edition your instance name is "computername\SQLExpress")

Enjoy!

Friday, March 13, 2009

IE7 vs ThisBlog (fixed)

Explorer 7(fixed):


... I have upgraded the css! :)

IE7 vs ThisBlog

Take a look...

Firefox:

Safari:

Explorer 7:

... ok, I'll try to fix it or maybe not! :(

Thursday, February 19, 2009

GridLines style in Gridview

A fast way to change the style of the gridlines in a gridview:


Protected Sub gwMyGw_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gwMyGw.PreRender

'PreRender event!
Dim tblStyle As New TableItemStyle()
tblStyle.BorderColor = Drawing.Color.White
tblStyle.BorderWidth = "2"

Dim row As TableRow
For Each row In Me.gwMyGw.Rows
Dim cel As TableCell
For Each cel In row.Cells
cel.ApplyStyle(tblStyle)
Next
Next

End Sub



Enjoy !

Saturday, November 29, 2008

Another bug in Visual Studio 2008 ?

I'm using VS 2008 with SP1...
For many, VS2008 is the best IDE for a programmers, but sometimes...

I was working with a GridView (previous post) and I had to change the name of a LinkButton from "Delete" to "Eliminate" a simple operation you could say... I also thought!
Unfortunately is not maintained the new "Text" property!
This problem does not exist with normal "Button", the property "Text" seems to work with this control and with the same conditions...

To perform the change, we need to go to the html source...

Friday, November 28, 2008

How to... use an Ajax ConfirmExtender in a GridView

A typical case to use an ajax-confirmbutton within a gridview is to delete a row on DB, let's take a look...

So, in our gridview, we have to create an "ItemTemplate", then add a LinkButton and the ajax ConfirmButtonExtender.
Set the property of our Extender like "TargetContolId" (= the LinkButton ID) and the "Confirm Text" (= Are You sure, continue?).
In this way we have connected the extender with the LinkButton !
Now we must connect the LinkButton with the data from the source (DataTable-method or a Control DataSource) in particular with an unic "row-ID"
Then go to Click-Event of the LinkButton to add the code to delete the row on DB...
something like:


Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'delete after Ajax-ConfirmButtonExtender
Dim lb As LinkButton = sender
Dim objDel As New clSessionTest(("ConnectionString"))
objDel.EliTempTest(lb.CommandArgument)
objDel = Nothing
End Sub

2012 | aspnet code by Michele | don't try this at home
Mirko Iodice Notageek
Vladimir Carrer Carrer web log