0

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.

4
Contributors
8
Replies
18
Views
3 Years
Discussion Span
Last Post by deceptikon
0

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.

1

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.

0

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

0

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.

Edited by deceptikon

0

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?

0

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).

Edited by deceptikon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.