-2

I wrote a C# program to output the contents what is contained in database tables, but when I append conn.open(), dialog box doesn't be created although there isn't any compile error. I already checked whether or not the setting of database information such as ip address, user name, pass word, and database name, but there isn't any misses. I wonder why it isn't executed as soon as appending conn.open():

public static string constring = "Data Source= 192.168.0.21; User=root; Password=admin;database=hwg;"; 
SqlConnection conn = new SqlConnection();
private string strConnString = "";

[public void ConnectDB]
strConnString = constring;

if (conn.State.ToString().Equals("Closed"))
{ 
    conn.ConnectionString = strConnString;
    conn.Open();     //problem on this line

    if (conn.State == ConnectionState.Open)
    {
    }
    else
    {
        conn.Close();
    }
}

[public DataTable GetDBTable(string sql)]
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);

return dt;

[public Form1()]
InitializeComponent();
db = new Con_database();
db.ConnectDB();

[private void button1_Click(object sender, EventArgs e)]/*If we click button1, the overall contents of applied table in database hwg is listed*/

   string sql = "SELECT * FROM id_repository";
   DataTable dt = db.GetDBTable(sql);

   DatabaseInquiry.DataSource = dt;
   db.ClosedDB();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    your connection string is wrong. It should be "Initial Catalog" in place of "database". Check out this link http://stackoverflow.com/questions/15631602/how-to-set-sql-server-connection-string – Mainak Jul 14 '16 at 07:11
  • The problem related in "conn.open()" is almost occurred by connection string error ? – Christopher Jul 14 '16 at 07:19
  • Yes. If your connection string isn't correct conn.Open() will produce error. – Mainak Jul 14 '16 at 07:20
  • The error type is " This expression caused side effects and will not be evaluated". Is it also one of connection string errors ? – Christopher Jul 14 '16 at 07:26
  • what other reason can you think of for error while you call conn.open() ? – Mainak Jul 14 '16 at 07:29
  • What dialog do you mean? SqlConnection.Open immediately connects to the database, using the connection string you've given. I wouldn't expect a dialog to appear. – Steve Cooper Jul 14 '16 at 07:36
  • 1
    Apparently the questioner has not even bothered to search a problem online and has straight away asked a question. To top it, he is actually doubting the correct answers given to him!!! – athar13 Jul 14 '16 at 08:00

1 Answers1

-1

I think you need to define USERID instead of USER in your connection string.

Old connection string:

    public static string constring 
= "Data Source= 192.168.0.21; User=root; Password=admin;database=hwg;"; 

New connection string:

    public static string constring = 
"Data Source= 192.168.0.21;Initial Catalog="your database name"; UserID=root; Password=admin;";
Mohamed
  • 806
  • 13
  • 30
  • Why you click downvote, I only tried to help you, you should thank me instead of downvote . if you have any note try to respect me in a comment as I respect you and tried to help ?!!! – Mohamed Jul 14 '16 at 07:40