Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net Namespace ......

Posted on 2012-03-19
5
Medium Priority
?
461 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Assisted Solution

by:Crashman
Crashman earned 500 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 500 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 30

Accepted Solution

by:
anarki_jimbel earned 500 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:Navneet
Navneet earned 500 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:Murray Brown
ID: 37742037
Thanks very much
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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