Solved

VB.net Namespace ......

Posted on 2012-03-19
5
422 Views
Last Modified: 2012-06-22
Hi

In the following code I am not quite sure what "Namespace JLSQLBrowser.Controls" is.
I got the code by converting the C# code below. Is this just a C# thing or does it do something in VB.net


Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.Excel

Namespace JLSQLBrowser.Controls
      Public Partial Class TpSqlEdit
            Inherits UserControl
            Public Sub New()
                  InitializeComponent()
            End Sub

            Private Sub buttonGetData_Click(sender As Object, e As EventArgs)
                  PopulateFromSql()
            End Sub
            Private Sub PopulateFromSql()
                  Try
                        ' DataTable Construction with Adapter and Connection
                        Dim conn = New SqlConnection(textBoxCS.Text)
                        Dim strSql = richTextBoxSQLEdit.Text
                        conn.Open()
                        Dim da = New SqlDataAdapter(strSql, conn)
                        Dim dt = New System.Data.DataTable()
                        da.Fill(dt)

                        ' Define the active Worksheet
                        Dim sht = TryCast(Globals.ThisAddIn.Application.ActiveSheet, Worksheet)

                        Dim rowCount = 0
                        progressBarGetData.Minimum = 1
                        progressBarGetData.Maximum = dt.Rows.Count

                        ' Loop thrue the Datatable and add it to Excel
                        For Each dr As DataRow In dt.Rows
                              rowCount += 1
                              For i As var = 1 To dt.Columns.Count
                                    ' Add the header the first time through
                                    If rowCount = 2 Then
                                          ' Add the Columns using the foreach i++ to get the cell references
                                          If sht IsNot Nothing Then
                                                sht.Cells(1, i) = dt.Columns(i - 1).ColumnName
                                          End If
                                    End If
                                    ' Increment value in the Progress Bar
                                    progressBarGetData.Value = rowCount
                                    ' Add the Columns using the foreach i++ to get the cell references
                                    If sht IsNot Nothing Then
                                          sht.Cells(rowCount, i) = dr(i - 1).ToString()
                                    End If
                                    ' Refresh the Progress Bar
                                    progressBarGetData.Refresh()
                              Next
                        Next
                  Catch ex As Exception
                        Throw New Exception(ex.ToString())
                  End Try

            End Sub
      End Class
End Namespace

'###########################################################
'###########################################################

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace JLSQLBrowser.Controls
{
    public partial class TpSqlEdit : UserControl
    {
        public TpSqlEdit()
        {
            InitializeComponent();
        }

        private void buttonGetData_Click(object sender, EventArgs e)
        {
            PopulateFromSql();
        }
        private void PopulateFromSql()
        {
            try
            {
                // DataTable Construction with Adapter and Connection
                var conn = new SqlConnection(textBoxCS.Text);
                var strSql = richTextBoxSQLEdit.Text;
                conn.Open();
                var da = new SqlDataAdapter(strSql, conn);
                var dt = new System.Data.DataTable();
                da.Fill(dt);

                // Define the active Worksheet
                var sht = Globals.ThisAddIn.Application.ActiveSheet as Worksheet;

                var rowCount = 0;
                progressBarGetData.Minimum = 1;
                progressBarGetData.Maximum = dt.Rows.Count;

                // Loop thrue the Datatable and add it to Excel
                foreach (DataRow dr in dt.Rows)
                {
                    rowCount += 1;
                    for (var i = 1; i < dt.Columns.Count + 1; i++)
                    {
                        // Add the header the first time through
                        if (rowCount == 2)
                        {
                            // Add the Columns using the foreach i++ to get the cell references
                            if (sht != null) sht.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                        }
                        // Increment value in the Progress Bar
                        progressBarGetData.Value = rowCount;
                        // Add the Columns using the foreach i++ to get the cell references
                        if (sht != null) sht.Cells[rowCount, i] = dr[i - 1].ToString();
                        // Refresh the Progress Bar
                        progressBarGetData.Refresh();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
             
        }
    }
}
0
Comment
Question by:murbro
5 Comments
 
LVL 8

Assisted Solution

by:Crashman
Crashman earned 125 total points
ID: 37739138
The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.
http://msdn.microsoft.com/en-us/library/z2kcy19k%28v=vs.80%29.aspx
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 125 total points
ID: 37739174
its nothing more than a way of organizing your code. However at runtime and for MSIL your type name will be name of your namespace + "." + typename, like in your case it will be:-

JLSQLBrowser.Controls.TpSqlEdit.

Namespaces are only known to vb/c#, IL is unaware of them....
so you need not to worry, just mention the namespace in imports wherever you intend to use TpSqlEdit type in your code, everything works fine...
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 125 total points
ID: 37739214
http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET

"In VB.NET, classes and other data structures for a specific purpose are grouped together to form a namespace. You can use the classes in a namespace, by simply importing the namespace. The Imports keyword is used to import a namespace to your project. .NET framework provides a rich set of built in classes, grouped together to various namespaces. In this lesson, we are using the System namespace. Import the System namespace (already available in .NET)."



Indeed, namespaces help us organise our code. Say, for example, I need a class called "Form". Not the Windows Form, something else for a production plant. How do I distinguish two classes called Form?! Here namespaces come.


Try to create a new project in VB, name it, e.g., Production, and create a new form, say, Form 1. Go to designer class, it states:
Partial Class Form1
    Inherits System.Windows.Forms.Form

Open in new window


So, form1 belongs to System.Windows.Forms.Form namespace.

Now add a new class Form.

    Public Class Form

    End Class

Open in new window


No problems with that. But to make it even more unic you may add it to a Namespace:
Namespace ProductionClasses

    Public Class Form

    End Class
End Namespace

Open in new window


In C# it is done automatically. Later you will need to reference your Form class using a namespace.

See the tutorial above.
0
 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 125 total points
ID: 37739230
Hi!

Every Namespace is a unique signature in itself
for example

Namespace Mubro.JLSQLBrowser.Controls.MyClass
is different from
Namespace Navneet.JLSQLBrowser.Controls.MyClass

Whereas
Namespace Mubro.JLSQLBrowser.Controls.MyClass
is different from
Namespace Mubro.JLSQLBrowser.Controls.MyClass
are same class.


Say you create two assemble (Namespace MyControlsAssembly) having your new control 'MyNewControl'
Now if you also have another project (Namespace MyFormAssembly)  new Control named same 'MyNewControl'

So if you have a Project and you add both these controls, then your project will give you compile error as it's confused which to refer in that case you have to mentions with it full Namespace at import level
using MyFormAssembly;
or
inline
MyFormAssembly.MyNewControl ctr = new MyFormAssembly.MyNewControl();


Thanks!
0
 

Author Closing Comment

by:murbro
ID: 37742037
Thanks very much
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 32
Import a txt file into a DataGridView and TextBox 20 37
write xml in vb.net 2 24
Get number of Files in Directory and Sub Directories 2 40
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question