Solved

VB.net Namespace ......

Posted on 2012-03-19
5
430 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 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 30

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:Murray Brown
ID: 37742037
Thanks very much
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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