Link to home
Start Free TrialLog in
Avatar of Takeoutdinner
Takeoutdinner

asked on

Convert VB to C#

Hi experts,

I want to convert following VB Code to C#.
Please help me.

Thanks,


Imports System.Configuration
Imports System.Web.Configuration
Imports Microsoft.VisualBasic

Public Class AppConfiguration

    Public Shared ReadOnly Property GKCConnectionStringSettings() As ConnectionStringSettings
        Get
            Dim connectionStringKey As String = _
                ConfigurationManager.AppSettings.Get("GKCConnectionString")
            Return ConfigurationManager.ConnectionStrings(connectionStringKey)
        End Get
    End Property

End Class


Imports System.Data
Imports Microsoft.VisualBasic

''' <summary>
''' The Content class represents a Content item in the database that can be displayed on the web site.
''' </summary>
Public Class GKCAccount
    Public Shared Function GetForums() As DataSet
        Return GKCAccountDB.GetForums()
    End Function
End Class

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class GKCAccountDB
    Public Shared Function GetForums() As DataSet
        Dim myDataSet As DataSet = New DataSet()
        Dim myFactory As DbProviderFactory = DbProviderFactories.GetFactory(AppConfiguration.GKCConnectionStringSettings.ProviderName)
        Using myConnection As DbConnection = myFactory.CreateConnection()
            myConnection.ConnectionString = AppConfiguration.GKCConnectionStringSettings.ConnectionString
            myConnection.Open()

            Dim myCommand As SqlCommand = New SqlCommand("my_Forums_GetForums", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter()
            myDataAdapter.SelectCommand = myCommand
            myDataAdapter.Fill(myDataSet)
            myConnection.Close()
            Return myDataSet
        End Using
End Class


    End Function

Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

You can try this online code convertor
http://www.developerfusion.com/tools/convert/vb-to-csharp/
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web.Configuration;

public class AppConfiguration
{

	public static ConnectionStringSettings GKCConnectionStringSettings {
		get {
			string connectionStringKey = ConfigurationManager.AppSettings.Get("GKCConnectionString");
			return ConfigurationManager.ConnectionStrings(connectionStringKey);
		}
	}

}

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

/// <summary>
/// The Content class represents a Content item in the database that can be displayed on the web site.
/// </summary>
public class GKCAccount
{
	public static DataSet GetForums()
	{
		return GKCAccountDB.GetForums();
	}
}


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data.Common;

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class GKCAccountDB
{
	public static DataSet GetForums()
	{
		DataSet myDataSet = new DataSet();
		DbProviderFactory myFactory = DbProviderFactories.GetFactory(AppConfiguration.GKCConnectionStringSettings.ProviderName);
		using (DbConnection myConnection = myFactory.CreateConnection()) {
			myConnection.ConnectionString = AppConfiguration.GKCConnectionStringSettings.ConnectionString;
			myConnection.Open();

			SqlCommand myCommand = new SqlCommand("my_Forums_GetForums", myConnection);
			myCommand.CommandType = CommandType.StoredProcedure;

			SqlDataAdapter myDataAdapter = new SqlDataAdapter();
			myDataAdapter.SelectCommand = myCommand;
			myDataAdapter.Fill(myDataSet);
			myConnection.Close();
			return myDataSet;
		}
	}

}

Open in new window

Avatar of Takeoutdinner
Takeoutdinner

ASKER

Thank you for posting.

I have errors with your advices: Looking forward to hearing answers soon.

1. I changed return line as follows:
  public static ConnectionStringSettings GKCConnectionStringSetting
  {
      get
      {
          string connectionStringKey = ConfigurationManager.AppSettings.Get("GKCConnectionString");
          return ConfigurationManager.ConnectionStrings["GKCConnectionString"];
      }
  }

2.      ForumsDB. has error with red undrline (was GKCAccountDB.) - It says "the name forumdb does not exist in the current context"
public static DataSet GetForums()
    {
        return ForumsDB.GetForums();
    }

3-1. AppConfiguration. has an error with red undrline - It says "the name forumdb does not exist in the current context"
3-2. new SqlCommand("my_Forums_GetForums", myConnection) has an error with red underline.

    public static DataSet GetForums()
    {
        DataSet myDataSet = new DataSet();
        DbProviderFactory myFactory = DbProviderFactories.GetFactory(AppConfiguration.GKCConnectionStringSettings.ProviderName);
        using (DbConnection myConnection = myFactory.CreateConnection())
        {
            myConnection.ConnectionString = AppConfiguration.GKCConnectionStringSettings.ConnectionString;
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand("my_Forums_GetForums", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter myDataAdapter = new SqlDataAdapter();
            myDataAdapter.SelectCommand = myCommand;
            myDataAdapter.Fill(myDataSet);
            myConnection.Close();
            return myDataSet;
        }
    }
>>  ForumsDB. has error with red undrline (was GKCAccountDB.) - It says "the name forumdb does not exist in the current context"
It seems that the object 'ForumsDB' is not declared. - Check whether any namespace missing OR declaration missing for that object.
This is ForumsDB:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data.Common;


/// <summary>
/// Summary description for ForumsDB
/// </summary>
public class ForumsDB
{
    public static DataSet GetForums()
    {
        DataSet myDataSet = new DataSet();
        DbProviderFactory myFactory = DbProviderFactories.GetFactory(AppConfiguration.GKCConnectionStringSettings.ProviderName);
        using (DbConnection myConnection = myFactory.CreateConnection())
        {
            myConnection.ConnectionString = AppConfiguration.GKCConnectionStringSettings.ConnectionString;
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand("my_Forums_GetForums", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter myDataAdapter = new SqlDataAdapter();
            myDataAdapter.SelectCommand = myCommand;
            myDataAdapter.Fill(myDataSet);
            myConnection.Close();
            return myDataSet;
        }
    }

}
When I enter AppConfiguration. , there is no follwed by IntelliSense comes up.
When I tried, this code is working. Check this code
class AppConfiguration
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for AppConfiguration
/// </summary>
public class AppConfiguration
{
	public AppConfiguration()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static ConnectionStringSettings GKCConnectionStringSettings
    {
        get
        {
            string connectionStringKey = ConfigurationManager.AppSettings.Get("GKCConnectionString");
            return ConfigurationManager.ConnectionStrings[connectionStringKey];
        }
    }


}

Open in new window


class GKCAccount
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for GKCAccount
/// </summary>
public class GKCAccount
{
	public GKCAccount()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static DataSet GetForums()
    {
        return GKCAccountDB.GetForums();
    }

}

Open in new window


class GKCAccountDB
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;

/// <summary>
/// Summary description for GKCAccountDB
/// </summary>
public class GKCAccountDB
{
	public GKCAccountDB()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static DataSet GetForums()
    {
        DataSet myDataSet = new DataSet();
        DbProviderFactory myFactory = DbProviderFactories.GetFactory(AppConfiguration.GKCConnectionStringSettings.ProviderName);
        using (DbConnection myConnection = myFactory.CreateConnection())
        {
            myConnection.ConnectionString = AppConfiguration.GKCConnectionStringSettings.ConnectionString;
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand("my_Forums_GetForums");
            myCommand.Connection = (SqlConnection)myConnection;
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter myDataAdapter = new SqlDataAdapter();
            myDataAdapter.SelectCommand = myCommand;
            myDataAdapter.Fill(myDataSet);
            myConnection.Close();
            return myDataSet;
        }
    }

}

Open in new window

Ok. Check my latest post - http:#34877028
In GKCAccountDB,
when I enter "AppConfiguration.", it doesn't bring any Intelsense items.
Should I enter something prior to AppConfiguration?
ASKER CERTIFIED SOLUTION
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Websites15 itself looks fine.
But when I imported it into my current program, AppConfiguratio. has same error.
Any thoughts?
Hi ,

I will start coding with yours. Then I shall know what's wrong with my current codes.
Thanks for your kind help
If my code help you, then why couldn't you give me some points!?
Sorry. I accepted with many thanks last week. But it was not done. I don't know why. I will do it again.

Thanks again.
I still have a problem for closing. Please ask administrator to close with my accept.  It says:
This question already has an auto close request. You must cancel the previous request before creating another.
I am very sorry to finalize it last week. I am giving you point with EE's help in chatting.
Very sorry.
I closed it with point last week. But it wasn't done properly. So I am closing it again with EE's help in chatting.

Thanks again
No probs!
Glad I could help
Raj