The various part identifier "System.Data.Datarowview" cannot be associated

Asked

Viewed 749 times

1

Try {

                if (cmbFavorecido.SelectedValue != null )
                {

                    txtID.Text = cmbFavorecido.SelectedValue.ToString();

                    cn = conexao.ConectarSqlServer();
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("select CNPJ_FORNECEDOR FROM FORNECEDOR WHERE ID_FORNECEDOR =  " + txtID.Text, cn);
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    ad.Fill(ds, "TbFornecedor");
                    txtCnpj.Text = ds.Tables["TbFornecedor"].Rows[0].ItemArray[0].ToString();

                }

1 answer

1

Try to do it this way.

 if (cmbFavorecido.SelectedValue != null)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = myConnString;
                try
                {
                    String txtID = cmbFavorecido.SelectedValue.ToString();
                    txtID.Text = txtID;

                    //cn = conexao.ConectarSqlServer();
                    //cn.Open();

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "select CNPJ_FORNECEDOR FROM FORNECEDOR WHERE ID_FORNECEDOR = @txtID";
                    cmd.Parameters.AddWithValue("@txtID", txtID);

                    SqlDataAdapter da = new SqlDataAdapter();
                    DataTable dt = new DataTable();

                    da.SelectCommand = cmd;
                    conn.Open();
                    da.Fill(dt);

                    txtCnpj.Text = dt.Rows[0][0];

                }
                catch (SqlException sqle)
                {
                    // MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
                }
                finally
                {
                    conn.Close();
                }
            }

The one important thing in your code that you need to be careful of, which is passing parameters. The way you’re doing you run the risk of someone using SQL injection, so user always Parameters.AddWithValue("@txtID", txtID); to pass parameters.

Thus the types of the parameters are appropriate to the types of the columns and the SQL statement is on account of ADO.NET. In addition, a good practice is to carry out validations.

  • Marconcillio Souza, when I go to execute the code, on the line where you have "ad.Fill(dt); gives the following error with the message : "Fill: Selectcommand.Connection property has not been initialized."

  • See now .... missed pass your connection string .

  • Cara had to change the name of the txtID variable because it was conflicting with the name of Textbox, in ad.Selectcommand was assigned cmd (object of sqlcommand) at the time of asking to demand in Textbox added . toString(); only it still gives error in ad.Fill(dt) this time with the following message "There is no mapping of the System.Windows.Forms.Textbox object type to a native type managed provider"Thanks in advance for this help

Browser other questions tagged

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