Hello

I am getting the following error Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct

with 'string' on this line:

public static string GetConnString()

and with "MyConnectionString" on this line:

return WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

I have looked around the Web and I have seen examples of

 public static String GetConnString()

with a capital 'S' and even

 public virtual String GetConnString()

but neither seem to be working for me.

Any help would be appreciated.

Recommended Answers

All 8 Replies

It sounds more like your class definition is incorrect. Can you post up the whole class?

Thanks for your reply.

This is all the script in the aspx.cs file

using Microsoft.AspNet.Identity;
using System;
using System.Linq;
using System.Web.UI;
using Website1;

public static string GetConnString()
{
return WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
}

string ConnString = Utils.GetConnString();
string SqlString = "INSERT INTO university (username, password, strEmail) VALUES (@username,@password, @strEmail)";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("username", txtusername.Text);
cmd.Parameters.AddWithValue("password", txtpassword.Text);
cmd.Parameters.AddWithValue("strEmail", txtstrEmail.Text);
conn.Open();
cmd.ExecuteNonQuery();
}
}

Thanks again.

A namespace and class definition are missing.

but neither seem to be working for me.

On a side note, String and string are equivalent. string is a keyword alias for System.String.

Hello pritaeas

Thanks for your reply.

Something like connection to replace string?

Not sure about the namespace.

My PC got a blue screen this morning and I need a new hard drive, so I will try it out over the next 3-4 days when I can replace the hard drive and download VS again.

Thanks

Not sure about the namespace.

You don't need a namespace, but you must have a class. C# has no concept of top level methods or fields, they're all members of a class.

Thanks for letting me know abou string and String - thought it might hae been case-sensitive.

So public static Connection.GetConnString() might work when I try it? I am taking it that that is a class?

Your methods must be wrapped in a class:

public class Connection
{
    public static string GetConnString()
    {
        return WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    }

    public static void InsertUser(string name, string pass, string email)
    {
        string ConnString = Utils.GetConnString();
        string SqlString = "INSERT INTO university (username, password, strEmail) VALUES (@username,@password, @strEmail)";

        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("username", name);
                cmd.Parameters.AddWithValue("password", pass);
                cmd.Parameters.AddWithValue("strEmail", email);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
}

However, since your code refers to text boxes, I'm assuming the class already exists as a Form, in which case you only need to add methods to that class (probably called Form1).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.