Tip 7: Check for SQL Server Database
This
tip checks for the existence of a SQL Server database. While it may seem a little complex, just break it down by variables and objects and it will be as easy as the rest. It's another good example of using the Object element in your snippet.
Function SQLDatabaseExists(ByVal objConnection As _
SqlConnection, ByVal strDatabaseName As String) _
As Boolean
'DEMO USAGE:
' Dim objConn As New SqlConnection _
' ("data source=sqlservername;user " & _
"id=sa;password=mypassword;Initial " & _
"Catalog=master;Connection Timeout=120")
' objConn.open()
' Debug.WriteLine(SQLDatabaseExists( _
' objConn, "MyDatabase"))
' objConn.Close()
Dim objCommand As SqlCommand
Dim i As Integer
Try
objCommand = New SqlCommand( _
"SELECT COUNT(*) FROM master..sysdatabases " & _
"WHERE [name]='" & strDatabaseName & "'", _
objConnection)
i = objCommand.ExecuteScalar
Return i > 0
Catch ex As Exception
Throw ex
End Try
End Function
Tip 8. Retrieve a Named DataSet
I chose
this function because it retrieves a named DataSet using C#, a language that I have not covered in these two snippet articles. Including it makes it pretty easy to see that snippets work the same way for C# as for VB.NET.
public System.Data.DataSet
dsReturnedDataSetFrom(
System.Data.SqlClient.SqlConnection
sqlOpenConn, System.Data.SqlClient.SqlCommand
TheSQLCommand, string TheQueriedTable,
String TheDatasetName)
{
TheSQLCommand.Connection = sqlOpenConn;
System.Data.SqlClient.SqlDataAdapter NewDA =
new System.Data.SqlClient.SqlDataAdapter
(TheSQLCommand);
DataSet NewDS = new DataSet();
TheDatasetName.Trim();
NewDS.DataSetName=TheDatasetName;
NewDA.Fill(NewDS, TheQueriedTable);
NewDA.Dispose();
return NewDS;
}
Tip 9: Export DataGrid to XML
This
tip is a cool little ASP.NET function written in C# that exports the content of a DataGrid to an XML file and sends that to the client.
ArrayList al = new ArrayList();
for(int i=0;i<=10;i++)
{
al.Add(i+"");
}
DataGrid1.DataSource=al;
DataGrid1.DataBind();
Response.Clear();
Response.AddHeader(
"content-disposition",
"fileattachment;filename=test.xml");
Response.ContentType="text/xml";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
Tip 10: Counting TreeView Nodes Recursively
In this final
function the code sets up a recursive loop to count the number of checked nodes in a given TreeView.
' Return the number of checked nodes within any node
' of a TreeView
Public Function CountCheckedNodes( _
ByVal rootNode As TreeNode) As Integer
Dim count As Integer = 0
' count the root node, if checked
If rootNode.Checked Then count = 1
' check the child nodes, by recursively calling
' this function
Dim tvn As TreeNode
For Each tvn In rootNode.Nodes
count += CountCheckedNodes(tvn)
Next
Return count
End Function
You can
download and install the tips featured in this article as a preconfigured snippets file, giving you the option to separate the snippets you want into individual snippet files. If you don't want to install all the snippets in the file, here's how to remove the ones you don't want.
Each individual snippet is contained wholly within its own
<Snippet> element. To remove snippets you don't want, first check the
<References> and
<Imports> nodes. If any are only used by the snippet you are deleting, then you can delete the corresponding
<Reference> and
<Import> nodes as well; however, bear in mind from the example in Tip 2 that although there were three functions in one snippet file, all of which required the same assembly and namespace, the snippet file included it only once. So, be sure that the
<Reference> or
<Imports> elements you delete aren't required by any other code snippets in the file.
After removing those, simply delete all the XML from the opening
<Snippet> to the closing
</Snippet> tags for each item you want to remove. Save the file. You can test your changes both by using the Test feature in the Snippet Editor, or by simply inserting them into a Visual Studio project and taking advantage of the real-time debugging features.