Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can't finish my homework. I get an error by this line:
C#
dr = cmd.ExecuteReader();

That is my source code. How do I fix this error?
C#
MySqlDataReader dr;
private void findData_btn_Click(object sender, EventArgs e)
{
    db_connection();
    cmd = new MySqlCommand("Select * from googolplex.person where Name='" + name_txt.Text + "'", connect);
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        try
        {
            name_txt.Text = dr["Name"].ToString();
            surname_txt.Text = dr["Surname"].ToString();
            age_txt.Text = dr["Age"].ToString();
            country_txt.Text = dr["Country"].ToString();
            job_txt.Text = dr["Job"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}


What I have tried:

C#
MySqlCommand mcd;
MySqlDataReader mdr;
string s;
private void findData_btn_Click(object sender, EventArgs e)
{
    try
    {
        s = "select * from googolplex.person where id=" + findData_txt.Text;
        mcd = new MySqlCommand(s, connect);
        mdr = mcd.ExecuteReader();
        if (mdr.Read())
        {
            name_txt.Text = mdr.GetString("Name");
            surname_txt.Text = mdr.GetString("Surname");
            age_txt.Text = mdr.GetString("Age");
            country_txt.Text = mdr.GetString("Country");
            job_txt.Text = mdr.GetString("Job");
        }
        else
        {
            MessageBox.Show("Query is failed.","ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Posted
Updated 30-Apr-16 17:47pm
v3
Comments
Herman<T>.Instance 29-Apr-16 16:58pm    
set the try{
before cmd =
naperva 29-Apr-16 17:06pm    
Which? MySqlCommand or directly set to execute?
Herman<T>.Instance 29-Apr-16 17:00pm    
what error do you get?
naperva 29-Apr-16 17:05pm    
It's left to give error. When I click the button nothing change.
Herman<T>.Instance 29-Apr-16 17:11pm    
do you understand anything of c# or programming in general?

You have not provided enough information to get any useful help.
(You might have figured that out yourself from the comments)

You need to realize that no one can execute your code. We don't know which database your are using in the first place.
Therefor, leaving out crucial information like where the variable connect is declared and initialized doesn't make it easy to give a good answer. (might it be db_connect?)

You also need to provide the error message you get, if any, otherwise it is impossible to help you figure out what is going wrong.

In order to solve your problem you need to use the debugger and set a break point at the first line in the method.

Then you step through the code and see what happens with your variables and statements.

1. The first thing I would check is to see if the variable connect has a value != null.
2. Then you need to make sure that your SQL statement actually gives back any result.
(is the statement if (dr.Read()) ever entered.


At last.
It is always bad to use concatenated SQL commands, due to the vulnerability to SQL injection attacks. See SQL injection - Wikipedia, the free encyclopedia[^]

Use parameterized queries instead. See for example MySQL :: MySQL Connector/Net Developer Guide :: 5.9.1 Preparing Statements in Connector/Net[^]
 
Share this answer
 
As per your "Additional Information" in the comments -

Most DBMS do not allow multiple data readers to be opened on a single connection at the same time. MySQL and SQLServer does allow it, but the connection needs be established with the "multipleactiveresultsets=true" statement as part of the connection string.

Personally I tend to create a clone of the original connection if I need this functionality. It is more reliable and less prone to error. It also allows for actions being performed on a different thread.
C#
using (MySqlConnection connection2 = (MySqlConnection)connection.Clone()) {
    //... all your stuff using 'connection2' instead of 'connection'
}
 
Share this answer
 
Comments
Midi_Mick 30-Apr-16 23:51pm    
Useful information about multiple data readers on a single connection can be found here:

https://blogs.msdn.microsoft.com/angelsb/2004/09/07/ado-net-2-0-multiple-active-resut-sets-per-connection-in-sql-server-2005-mars-faq/

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