Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have problem about database connectivity.please help me this regard
code is

C#
private void button5_Click(object sender, EventArgs e)
        {
            string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";

            SqlConnection con = new SqlConnection(str);
           try{
            con.Open();
           }
            catch(Exception ){

                MessageBox.Show("cannot connect");
            }
           SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
           cm.Connection = con;
           SqlDataReader reader = null;
           reader = cm.ExecuteReader(); // error showing in here it is mentioning connection closed
           while (reader.Read())
           {
               if (textBox1.Text ==(reader["username"].ToString())  && textBox2.Text == (reader["password"].ToString()))
               {
                   label8.Visible = true;
               }
               else
               {
                   label9.Visible = true;
                   textBox1.Text = "";
                   textBox2.Text = "";
               }
           }
        }
Posted
Updated 8-Apr-15 7:02am
v2
Comments
Sascha Lefèvre 8-Apr-15 13:05pm    
The MessageBox saying "cannot connect" doesn't pop up?
nuke_infer 9-Apr-15 2:13am    
yes it is opening cannot connect box
Sascha Lefèvre 9-Apr-15 2:27am    
Did you follow OriginalGriff's advice?
ZurdoDev 8-Apr-15 13:17pm    
I suggest step through the code because something doesn't look right. You open the connection so calling ExecuteReader() should not fail.
Richard Deeming 8-Apr-15 15:18pm    
You appear to be storing passwords in plain text. That's a very bad idea. You should only ever store a salted hash of the password.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

That's an SDF file, not a SQL Server database.
Use SQLCE classes instead of SQL server:
SqlCeConnection[^]
SqlCeCommand[^]
And so forth.
 
Share this answer
 
Comments
Sascha Lefèvre 8-Apr-15 14:27pm    
Good spot, +5. But why would his error only pop up on ExecuteReader?
OriginalGriff 8-Apr-15 14:33pm    
Probably because SQL server didn't look at the file until it had something to do...
Sascha Lefèvre 8-Apr-15 14:35pm    
Makes sense I guess ;)
nuke_infer 9-Apr-15 3:22am    
yes i followed your guide and its successful. thank you orig :)
OriginalGriff 9-Apr-15 4:12am    
You're welcome!
Please use this ..
C#
private void button5_Click(object sender, EventArgs e)
        {
            string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";
 
            SqlConnection con = new SqlConnection(str);
           try{
            con.Open();
           }
            catch(Exception ){
 
                MessageBox.Show("cannot connect");
            }
           SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
           
           SqlDataReader reader = null;
           con.Open();
           reader = cm.ExecuteReader(); 
           con.Close();
           while (reader.Read())
           {
               if (textBox1.Text ==(reader["username"].ToString())  && textBox2.Text == (reader["password"].ToString()))
               {
                   label8.Visible = true;
               }
               else
               {
                   label9.Visible = true;
                   textBox1.Text = "";
                   textBox2.Text = "";
               }
           }
        }



Thanks
AARIF SHAIKH
 
Share this answer
 
Comments
nuke_infer 9-Apr-15 2:40am    
AARIF : now it is showing con.open() place sql exception
private void button5_Click(object sender, EventArgs e)
{
string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";

SqlConnection con = new SqlConnection(str);
try{

if(con.State==ConnectionState.Open)
con.Close();
con.Open();
}
catch(Exception ){

MessageBox.Show("cannot connect");
}
SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
cm.Connection = con;
SqlDataReader reader = null;
reader = cm.ExecuteReader(); // error showing in here it is mentioning connection closed
while (reader.Read())
{
if (textBox1.Text ==(reader["username"].ToString()) && textBox2.Text == (reader["password"].ToString()))
{
label8.Visible = true;
}
else
{
label9.Visible = true;
textBox1.Text = "";
textBox2.Text = "";
}
}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900