Print Page | Close Window

Needs Help with C# Code

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=13300
Printed Date: 29 March 2026 at 7:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Needs Help with C# Code
Posted By: Misty
Subject: Needs Help with C# Code
Date Posted: 11 January 2005 at 1:51am
I've decided to convert one of my VB.Net pages to C#. I used a C# converter at http://www.developerfusion.com/utilities/convertvbtocsharp.aspx - http://www.developerfusion.com/utilities/convertvbtocsharp.a spx . It has been helpful. But it is not 100% accurate.
 
I need help with the following lines of code:
 
1.)
VB.Net:     Private Function ConnectionString () as String
    
        Dim servername, username, password, databasename As String
        servername = ""
        username = ""
        password = ""
        databasename = ""
        Dim conString As String
        conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename)
        Return conString
    
    End Function
 
C#:   private string ConnectionString()
{
 string servername;
 string username;
 string password;
 string databasename;
 servername = "";
 username = "";
 password = "";
 databasename = "";
 string conString;
 conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename);
 return conString;
}
 
I got the following error message regarding the bolded code in C#: Compiler Error Message: CS0246: The type or namespace name 'conString' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 28:  databasename = "spastoredirectory"; 
Line 29:  string conString; 
Line 30:  conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename); 
Line 31:  return conString; 
Line 32: }
 
2.)
VB.Net: strChosenState = request.params("State") (worked fine in VB.Net)
 
C#: strChosenState = request.params("State");
 
I got the following error message in C#:
Compiler Error Message: CS1041: Identifier expected, 'params' is a keyword

Source Error:

Line 38:        
Line 39: 	     //Get incoming querystring values 
Line 40:          strChosenState = request.params("State"); 



Replies:
Posted By: Mart
Date Posted: 11 January 2005 at 2:18am
1)

try changing it to

conString = string.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename);

2)

strChosenState = Request.QueryString["State"];


Posted By: michael
Date Posted: 11 January 2005 at 4:34pm
Misty,
 
I had to delete you last post as you left your Providers SQL Server Connection info in there incl. Password. So here is Misty's last post again without that
 
Originally posted by Misty Misty wrote:

I have another problem with the following line of code:
 
System.Data.SqlClient.SQLConnection objConnect = new System.Data.SqlClient.SQLConnection(ConnectionString());
 
I got the following error message: Compiler Error Message: CS0234: The type or namespace name 'SQLConnection' does not exist in the class or namespace 'System.Data.SqlClient' (are you missing an assembly reference?)

Source Error:

Line 41: { 
Line 42:              string strConnect;   
Line 43:              System.Data.SqlClient.SQLConnection objConnect = new System.Data.SqlClient.SQLConnection(ConnectionString()); 
Line 44:              System.Data.SqlClient.SQLCommand objCommand = new System.Data.SqlClient.SQLCommand(); 
Line 45:              string strSQL; 
 
ConnectionString in the red line is supposed to come from the following code:
 
private string ConnectionString()
{
 string servername;
 string username;
 string password;
 string databasename;
 servername = "<deleted>";
 username = "<deleted>";
 password = "<deleted>";
 databasename = "spastoredirectory";
 string conString;
conString = string.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename);
 return conString;
}


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 11 January 2005 at 4:36pm
Dor some reason it did not let me edit above post as a Moderator (bug? Tongue)
 
Anyway, there is nothing wrong with that line itself, must be another problem..
What do you get when you but
 
using System.Data.SQLClient
 
into the top of the cs file and the don't reference the whole class but the one method?


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date Posted: 11 January 2005 at 5:40pm
Michael,
I was in a big hurry when I pasted the code earlier. I need to be more careful next time.
 
Here's more code:
 

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
 
 <script language=C# runat=server>
 

private void Page_Load(object sender, System.EventArgs e)
{
 if (!Page.IsPostBack) {
   BindDataList();
   BindAreaList();
 }
}
 
private string ConnectionString()
{
 string servername;
 string username;
 string password;
 string databasename;
 servername = "<deleted>";
 username = "<deleted>";
 password = "<deleted>";
 databasename = "spastoredirectory";
 string conString;
conString = string.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename);
 return conString;
}
 

//---------------------------------------------
// name: BindAreaList()
//---------------------------------------------
void BindAreaList()
{
              string strConnect;  
              System.Data.SqlClient.SQLConnection objConnect = new System.Data.SqlClient.SQLConnection(ConnectionString());
              System.Data.SqlClient.SQLCommand objCommand = new System.Data.SqlClient.SQLCommand();
              string strSQL;
              System.Data.SqlClient.SQLDataAdapter dtaArea = new System.Data.SqlClient.SQLDataAdapter();
              DataSet dtsArea = new DataSet();
              string strChosenState;
              string FullState;
 
    
              objConnect.Open();
             
      
      //Get incoming querystring values
         strChosenState = Request.QueryString["State"];
    
              //Start SQL statement
              strSQL = "Select * From Area, State";
              strSQL = strSQL + " where Area.State = State.State";
              strSQL = strSQL + " and Area.State = '" + strChosenState + "'";
              strSQL = strSQL + " Order By AreaName";
  
       
        //Set the Command Object properties
        objCommand.Connection = objConnect;
        objCommand.CommandType = CommandType.Text;
        objCommand.CommandText = strSQL;
        //Create a new DataAdapter object
        dtaArea.SelectCommand = objCommand;
        //Get the data from the database and
        //put it into a DataTable object named dttSpa in the DataSet object
        dtaArea.Fill(dtsArea, "dttArea");
        //Set the DataSource property of the DataGrid
        dtlArea.DataSource = dtsArea;
  
  //Set module level variable for page title display
        FullState = dtsArea.Tables(0).Rows(0).Item("StateName");
         
        //Bind all the controls on the page
        dtlArea.DataBind();
  
        //Display chosen StateName
        lblState.Text = "Find a Spa Store in  " & FullState;
 
        //Show chosen category in our navigation breadcrumb
        lblChosenState.Text = FullState;

        //Display category choice in page title
        litTitle.Text = "Find a Spa Store in " & FullState;
       
        objCommand.ExecuteNonQuery();
       
        //this is the way to close commands
        objCommand.Connection.Close();
       
        objConnect.Close();

}


Posted By: Misty
Date Posted: 12 January 2005 at 12:13am
Mart was right about the querystring. I tested it. It worked fine in a simpler ASP.Net page that uses C#.
 
However, I think that there's a problem with the line of code: conString = string.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename);
 
I tried doing what Mart said to do, but it will not work. I believe that this is why I'm getting the error message. The syntax in C# is different from VB.Net. But I have a desire to learn C#.  Can someone please give me some ideas on how to fix the line of code above?


Posted By: Mart
Date Posted: 12 January 2005 at 2:25am
Try this...


string[3] params;

params[0] = servername;
params[1] = username;
params[2] = password;
params[3] = databasename;

conString = string.Format("server={0};User ID={1};Password={2};Database={3};", params);


The problem is string.Format takes a maximum of 3 arguments IIRC


Posted By: Misty
Date Posted: 12 January 2005 at 1:26pm
I tried doing what Mart suggested, but I am getting the following error message now: Compiler Error Message: CS1525: Invalid expression term 'string'

Source Error:

Line 21: { 
Line 22:  
Line 23:  string[3] params;
Line 24:  params[0] = servername;
Line 25:  params[1] = username;
My code is:
 

string[3] params;
 params[0] = servername;
 params[1] = username;
 params[2] = password;
 params[3] = databasename; 
 servername = "";
 username = "";
 password = "";
 databasename = "";
 string conString; 
 conString = string.Format("server={0};User ID={1};Password={2};Database={3};", params);
 return conString;



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net