0

just learning asp.net with c# and my code is throwing this exception when im trying to open a connection to my database:

string connectionString ="DataSource=localhost,3306;Database=somedatabase;Uid=username;Pwd=password;";
using(SqlConnection con = new SqlConnection(connectionString))
{
    con.Open(); // Exception Thrown Here
}

Seems relatively simple, so ive really no clue why this is happening.

one other thing;

all this code is inside tags on the webpage (not really sure if that makes any difference, quite new to asp.net)

Cheers!

edit-

Hey, here is the rest of it. Im going to investigate habib's solution.

I am missing some assemblies (mysql), but ill get back when ive tried it out.

<%@ Language=C# %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="MySql" %>

<HTML>
   <script runat="server" language="C#">
   void MyButton_OnClick(Object sender, EventArgs e)
   {

   }

        void Page_Load(Object sender, EventArgs e)
           {
                // GET THE USERNAME AND PASSWORD FROM THE CLIENT
                NameValueCollection nvc = Request.Form;

                string username = nvc["username"];
                string password = nvc["password"];
                Login(username, password);
           }

           void Login(string username, string password)
           {
                string connectionString = "Data Source=localhost,3306;Database=something;Uid=somename;Pwd=somepass;";
                using(MySqlConnection con = new MySqlConnection(connectionString))
                {
                    con.Open();
    /*
                if (con.State != ConnectionState.Open)
                    return;

                try
                {
                    SqlDataReader   reader = null;
                    SqlCommand      command = new SqlCommand("SELECT * FROM accounts WHERE username=@username AND password=@password", con);

                    reader = command.ExecuteReader();
                    Response.Write(reader[0]);
                }
                catch (Exception e)
                {
                    Response.Write(e.ToString());
                }

                con.Close();*/
        }
        }
5
  • 1
    Is that really all your code? Commented Sep 5, 2013 at 17:00
  • Can you post more of your code? Commented Sep 5, 2013 at 17:05
  • I try to create a SqlConnection with this connectionstring and return this error "There is no support for the keyword: 'datasource'." Commented Sep 5, 2013 at 17:11
  • No, but I figured Id just post the problematic part :P Commented Sep 5, 2013 at 17:12
  • Just looking at your snippet above, what really stood out to me was the comma in your connection string after localhost. make sure your connection string is well formed: connectionstrings.com/mysql Commented Sep 6, 2013 at 2:31

1 Answer 1

2

You are trying to open MySQL database connection through SqlConnection, Use MySql.Data.MySqlClient.MySqlConnection

SqlConnection class is used for SQL Server connection.

Sign up to request clarification or add additional context in comments.

4 Comments

Does not explain the nullref exception.
@usr, I guess its because the OP is specifying a MySQL Connection string, the constructor for SqlConnection is returning null, hence the object con is null, BUT I am not sure about it.
A ctor never returns null, and the BCL should never crash internally. Very rare.
Seeing the updated comment by the OP, this is likely to be the solution. He probably has other funky stuff in his code that is not visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.