Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
con = new SqlConnection(connectionstring);


My code as follows;
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;


namespace Faculty_Schedule_Time_Table
{

        public class Global_Function
    {

            public SqlConnection con = new SqlConnection();
            private string connectionstring ="Data Source=NARASIMAN;connect timeout=120;initial catalog=HIMT;userid=sa;";

            private SqlCommand cmd;
            private SqlDataReader dr;

            public string error;

            public Global_Function()
            {

            }

            public void BindCon()
            {
                con = new SqlConnection();
                con.ConnectionString = connectionstring;
                con.Open();

            }

            public void insertdata(String SQL)
            {
                try
                {
                    BindCon();
                    cmd = new SqlCommand(SQL, con);
                    cmd.ExecuteReader();
                }
                 catch(Exception e1)
                  {
                   error = e1.Message.ToString();
                  }
            }
            public SqlDataReader ReadSql(string SQL)
            {

            con = new SqlConnection(connectionstring);
            con.Open();
            cmd  = new SqlCommand(SQL,con);
            dr = cmd.ExecuteReader();
            return (dr);
            
        }
           
            public string CntString()
        {
            return connectionstring;
        }
    }
}


When i run the above it shows the below error as follows;
Keyword not supported: 'userid'.

it shows the error in below line as follows;
C#
con = new SqlConnection(connectionstring);

Please help me.

I am using sqlserver 2000 database.

how to username and password of sqlserver 2000 database.

[Edit]Code block added[/Edit]
Posted
Updated 3-Mar-13 3:24am
v3
Comments
[no name] 3-Mar-13 8:53am    
www.connectionstrings.com

1 solution

It's not a global function - C# has no concept of global functions or variables. I would suggest that you look into the static keyboard which would make these easier to use as you will not need an instance to access them.

To get rid of your existing problem, you will probably need to add a space in teh word "userid":
C#
private static string connectionstring ="Data Source=NARASIMAN;connect timeout=120;initial catalog=HIMT;user id=sa;";

But you may find you need a password as well.

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
Per Söderlund 3-Mar-13 17:13pm    
static keyboard = static keyword?
OriginalGriff 3-Mar-13 17:23pm    
:laugh:
Android predictive text!
Per Söderlund 3-Mar-13 17:27pm    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900