Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Needs Help with C# Code
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Needs Help with C# Code

 Post Reply Post Reply
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Needs Help with C# Code
    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.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"); 
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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"];
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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;
}
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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();

}


Edited by Misty - 11 January 2005 at 5:41pm
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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;


Edited by Misty - 12 January 2005 at 1:27pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.