using of the unnecessary visual studio

Asked

Viewed 121 times

3

Does anyone know why it gets like this?

Imagem

It shows that using is not being used, but I do not understand why it is not recognized if I am using in the function below.

inserir a descrição da imagem aqui

Resolution:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MySql.Data.MySqlClient;
    using System.Data;

    namespace MySqlServerDemo
    {
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

       public void BindData()
        {
            MySqlConnection con = new MySqlConnection("server=localhost;user    id=root;database=test");
            con.Open();

            MySqlCommand cmd = new MySqlCommand("select * from person", con);
            MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);

            GridView1.DataSource = ds;
            GridView1.DataBind();
            cmd.Dispose();
            con.Close();
        }  
    }
}
  • 1

    what use function you are using?

  • 1

    These alerts appear because it is not good practice to keep calling libraries that you are not using in this scope. In declaring your class when inheriting class information Page, there is no need to inform all namespace there, you can just leave the name of the class as in the example below. public partial class WebForm1 : Page One way to correct these alerts is to right-click on the highlighted item and use the function Organize Usings -> Remove Unecessary Usings or use the shortcut CTRL + .

  • @Marconciliosouza connect mysql daods database

  • @Leandroaraujo I did what I said and enabled only one using, I need to enable all, I started using the visual studio now, so I’m having these silly doubts.

  • 1

    if you remove Mysql.Data.Mysqlclient from the error?

  • gets the same mistakes

  • 1

    you who say ( not being used) for me this is no mistake.

  • @Marconciliosouza look at the image I posted now, gets those errors because using Mysql.Data.Mysqlclient is "disabled"

  • 1

    It’s not that, your system is in error.. what you can see right away is the declaration of the Bindata method, you forgot the () at the end. the right one would be public void BindData() { ..... }, edit your question with the code in if instead of the image so it is more readable.

  • Okay, I’ll do that, thank you!

  • 2

    @Lorena seems to me that the declaration of its Binddata function is incorrect, try the following public void BindData()

  • 1

    @Lorena if change line MySqlCommand =cmd = new MySqlCommand() For MySqlComman cmd = new MySqlComman() I believe it will work, and the same for MySqlAdapter

  • @Renancarlos I went to realize that after, already I am, thank you very much!

  • Solved! Thank you very much, guys. @Marconciliosouza

  • @Renancarlos was worth it!

  • @Leandroaraujo was worth!

Show 11 more comments

3 answers

5


Your problem is occurring due to incorrect method creation BindData, at the completion of your method you forgot the ().

See the correct statement.

public void BindData() 
{ 
     // seu código ... 
}

Compile (click the button inserir a descrição da imagem aqui Build Solution) your project and fix all errors that appear, after that the references that are not actually used will be in lighter color (almost transparent) these references can be removed smoothly for the application, you can remove one by one or use the button (inserir a descrição da imagem aqui) to remove all unused reverences and order those remaining.

1

Well, in addition to @Marconciliosouza’s reply, there are more errors in your code.

Look at these lines:

MySqlCommand = cmd = new MySqlCommand("select * from person", con);
MySqlDataAdapter = adp = new MySqlDataAdapter(cmd);

The correct statement is:

MySqlCommand cmd = new MySqlCommand("select * from person", con);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);

That is, your method BindData should stay that way:

public void BindData()
{
    MySqlConnection con = new MySqlConnection("server=localhost;user    id=root;database=test");
    con.Open();

    MySqlCommand cmd = new MySqlCommand("select * from person", con);
    MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();
    cmd.Dispose();
    con.Close();
 } 

With these modifications, I believe it will work normally. If you have any other questions, I’m available to help.

  • Okay @Renancarlos, thank you so much for your help, it’s all set!

1

Complementing the answer, the clearest "usings" theoretically are not being used and can be removed.

Theoretically because if there is a syntax error, the VS pre-compiler can mark a certain line of using as not needed when it actually is (after correction of the syntax).

It is good practice to remove excess lines to keep code cleaner.

  • OK @Fabriciogs, I did this and everything is going well, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.