| | |
Connection must be valid and open
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I am getting an "Connection must be valid and open" error. After some research I have found two possible reasons for this: malformed sql or bad connection string. Since the error is being thrown on a second .executescalar statement I will assume the connection string is working. I have two questions. First - if I can cut and paste the sql statement into mySql's query browser and it returns the information, is the sql malformed? (e.g. select user_id from cms.accounts where user_id='scott@scottrider.com') Next - are there other reasons for the error message.
Last edited by scott_rider; Jun 26th, 2006 at 10:09 pm.
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace login_menu{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class login_form: System.Web.UI.Page{
protected System.Web.UI.WebControls.TextBox user_id;
protected System.Web.UI.HtmlControls.HtmlInputText password;
protected System.Web.UI.WebControls.Label error_display;
private void Page_Load(object sender, System.EventArgs e){
if(Page.IsPostBack){
string user_id_string=user_id.Text;
string password_string=password.Value;
string sql=mySql.sql_login(user_id_string,password_string);
error_display.Text="";
MySqlConnection mysql_db_access=mySql.connect;
MySqlCommand mysql_db_query=new MySqlCommand(sql);
mysql_db_query.Connection=mysql_db_access;
mysql_db_access.Open();
int test_account_id=Convert.ToInt16(mysql_db_query.ExecuteScalar());
sql=mySql.sql_test_user_id(user_id_string);
mysql_db_query=new MySqlCommand(sql);
string test_user_id=Convert.ToString(mysql_db_query.ExecuteScalar());
if(test_account_id==0){
#region test_user_id
if(test_user_id==System.String.Empty){
// no user account error message
// display link to new account web page
error_display.Text="Invalid user account <a href=\"<A href="http://www.yahoo.com\">new">http://www.yahoo.com\">new account</a>";
}else{
// wrong password error message
// display link to send password to email account
error_display.Text="Invalid password <a href=\"<A href="http://www.mama.com\">new">http://www.mama.com\">new account</a>";
}
#endregion
}else{
// successful login
// set cms access to granted
mysql_db_query.Connection.Close();
Response.Redirect("http://www.google.com");
}
}
}
}
}
public class mySql{
public static MySqlConnection connect{
get{
return new MySqlConnection(mySql.connect_string);
}
}
public static string connect_string{
get{
return "server=localhost; database=cms; uid=root; port=3306; password=;";
}
}
public static string sql_login(string user_id, string password){
StringBuilder sql = new StringBuilder();
sql.Append("select account_id from cms.accounts ")
.Append("where user_id='" + user_id + "' ")
.Append("and user_password='" + password + "'");
return sql.ToString();
}
public static string sql_test_user_id(string user_id){
StringBuilder sql=new StringBuilder();
sql.Append("select user_id from cms.accounts ")
.Append("where user_id='" + user_id + "'");
return sql.ToString();
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace login_menu{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class login_form: System.Web.UI.Page{
protected System.Web.UI.WebControls.TextBox user_id;
protected System.Web.UI.HtmlControls.HtmlInputText password;
protected System.Web.UI.WebControls.Label error_display;
private void Page_Load(object sender, System.EventArgs e){
if(Page.IsPostBack){
string user_id_string=user_id.Text;
string password_string=password.Value;
string sql=mySql.sql_login(user_id_string,password_string);
error_display.Text="";
MySqlConnection mysql_db_access=mySql.connect;
MySqlCommand mysql_db_query=new MySqlCommand(sql);
mysql_db_query.Connection=mysql_db_access;
mysql_db_access.Open();
int test_account_id=Convert.ToInt16(mysql_db_query.ExecuteScalar());
sql=mySql.sql_test_user_id(user_id_string);
mysql_db_query=new MySqlCommand(sql);
string test_user_id=Convert.ToString(mysql_db_query.ExecuteScalar());
if(test_account_id==0){
#region test_user_id
if(test_user_id==System.String.Empty){
// no user account error message
// display link to new account web page
error_display.Text="Invalid user account <a href=\"<A href="http://www.yahoo.com\">new">http://www.yahoo.com\">new account</a>";
}else{
// wrong password error message
// display link to send password to email account
error_display.Text="Invalid password <a href=\"<A href="http://www.mama.com\">new">http://www.mama.com\">new account</a>";
}
#endregion
}else{
// successful login
// set cms access to granted
mysql_db_query.Connection.Close();
Response.Redirect("http://www.google.com");
}
}
}
}
}
public class mySql{
public static MySqlConnection connect{
get{
return new MySqlConnection(mySql.connect_string);
}
}
public static string connect_string{
get{
return "server=localhost; database=cms; uid=root; port=3306; password=;";
}
}
public static string sql_login(string user_id, string password){
StringBuilder sql = new StringBuilder();
sql.Append("select account_id from cms.accounts ")
.Append("where user_id='" + user_id + "' ")
.Append("and user_password='" + password + "'");
return sql.ToString();
}
public static string sql_test_user_id(string user_id){
StringBuilder sql=new StringBuilder();
sql.Append("select user_id from cms.accounts ")
.Append("where user_id='" + user_id + "'");
return sql.ToString();
}
}
}
•
•
Join Date: Mar 2009
Posts: 4
Reputation:
Solved Threads: 0
make:
to
MySqlCommand mysql_db_query=new MySqlCommand(sql); to
MySqlCommand mysql_db_query=new MySqlCommand(sql,mysql_db_access); Last edited by peter_budo; Mar 18th, 2009 at 2:21 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
![]() |
Similar Threads
- Login Page Database connection to MSAccess (VB.NET)
- IE open repeatedly HELP PLEASE! (Viruses, Spyware and other Nasties)
- Internet connection problem (Networking Hardware Configuration)
- Connection open, but can't open Internet? (Networking Hardware Configuration)
- Ports won't open (Windows NT / 2000 / XP)
- Unable to open to the web page and very very low speed of internet. (C)
- SP2 Windows update; lost internet connection (HJT Log) (Viruses, Spyware and other Nasties)
- Internet explorer new windows dont open (Web Browsers)
Other Threads in the MySQL Forum
- Previous Thread: Confused beginner
- Next Thread: Problem with UTF8
Views: 5644 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design development distinct drupal ec2 eliminate email enter enterprise error eudora facebook form foss gartner gnu gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keywords kickfire laptop law linux maintenance managing mariadb metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlquery mysqlsearch news open-xchange opendatabasealliance opensource operand oracle pdf php priceupdating query referencedesign remove reorderingcolumns results resultset saas search sharepoint simpledb sourcecode spotify sql statement sugarcrm syntax techsupport transparency update





