Click to See Complete Forum and Search --> : can't connect to MS SQL express


whathappened9
01-03-2007, 06:01 PM
Hello,
I have SQLEXPRESS on my machine with a database named cccr. Through database explorer it is connected, but not when I run the website. here is the relevant code

static string connStr = "server=myMachine\\SQLEXPRESS;database=cccr;";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

lblError.Text = "";
string user = txtUser.Text.Trim();
string pass = txtPass.Text.Trim();
string strSQL = "SELECT count(KFUPM_ID) FROM Admin WHERE KFUPM_ID = '" + user + "' AND PassW = '" + pass + "'";

SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = strSQL;
dbCommand.Connection = conn;

int num = 0;

conn.Open();
try
{
num = Int16.Parse(dbCommand.ExecuteScalar().ToString());
}
catch (Exception) { }
conn.Close();




Whenever I click , I get unhandled exception pointing at
" conn.Open " . I would appreciate any comments.

thank you

russell
01-03-2007, 06:09 PM
connStr

Provider=SQLOLEDB.1;Password=pwd;Persist Security Info=True;User ID=uid;Initial Catalog=cccr;Data Source=machineNameOrIpAddress

if using machine name, dont use unc path -- just machine name. else use IP address.

also, make sure sql server set toi accept remote connections, mixed mode enabled and tcp/ip enabled

whathappened9
01-03-2007, 06:25 PM
Thank you russel for responding. I am not sure if I did set up a user name and a password. I thought by using Windows authentication I won't have to specify them in my connection string. Do I have to uninstall then reinstall again?

Thanks

russell
01-03-2007, 06:35 PM
no dont do that. if using integrated security, connStr should look like

Data Source=machineNameOrIp;Initial Catalog=databaseName;Integrated Security=true;
still may need to make sure of other things i mentioned above...

This link (http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx) may be useful

whathappened9
01-03-2007, 07:08 PM
It just won't happen. I give up :| . Maybe I should try doing database files within web developer. Anyhow thanks for your time

russell
01-03-2007, 08:14 PM
did u enable remote connections in surface area configuration tool? enabled tcp/ip?

whathappened9
01-03-2007, 08:32 PM
yeah I did, but didn't help. I started all over again, using web developer own files. It is working fine.

Thank you