Solved

VB.net Namespace ......

Posted on 2012-03-19
5
412 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now