Reputation: 2632
I'm a Beginner in c# so It's not encouraging to me see you voting down on my basic question...
This is a button to show in a message Box the result of my select from my BD.
I get errors : Argument '2': cannot convert from 'object' to 'string', Argument '3': cannot convert from 'object' to 'System.Windows.Forms.MessageBoxButtons'
Thank's for helping me!
private void Button1Click(object sender, EventArgs e)
{
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "Select id_Client,numéro_Teléphone from Clients";
SqlDataReader thisreader = thisCommand.ExecuteReader();
while (thisreader.Read())
{
MessageBox.Show(thisreader["id_Client"],thisreader["numéro_Teléphone"]);
}
thisreader.Close();
thisConnection.Close();
}
Upvotes: 0
Views: 127
Reputation: 3680
Couple things I would add here.
I don't think the call
thisreader["id_Client"].ToString()
is DBNull safe, meaning if the actual value back from the database is NULL this might cause a problem.
Secondly, your connection to the database server will not be closed until a user has clicked through all of the MessageBoxes. That might not be expected behavior. I almost always use a DataTable and a SqlDataAdapter to fill it, that way I know I have all my data back from the server and my connection has been closed so I am not using up extra resources on the server.
Also another things, since you are new to C# your
thisreader.Close();
thisConnection.Close();
might not get executed, you need to either do a
try / catch / finally
or a
using()
statement. Just giving you some best practices stuff to think about.
Upvotes: 2
Reputation: 94643
Try,
MessageBox.Show(thisreader["id_Client"].ToString() + " " +
thisreader["numéro_Teléphone"].ToString());
Read MSDN doc for more details.
EDIT:
Use System.Text.StringBuilder to append strings.
System.Text.StringBuilder sb=new System.Text.StringBuilder();
while (thisreader.Read())
{
sb.Append("\n" + thisreader["id_Client"].ToString() + " " +
thisreader["numéro_Teléphone"].ToString());
//or
//sb.Append(string.Format("\n{0} {1}",thisreader["id_Client"],thisreader["numéro_Teléphone"]));
}
MessageBox.Show(sb.ToString());
Upvotes: 2