how do I have information passed to the textbox from id

Asked

Viewed 75 times

0

He wanted the MRI of the student (primary key) to be filled the textbox with the information related to that student.

CREATE TABLE ALUNO(
RM_ALUNO INT NOT NULL,
NM_ALUNO VARCHAR (40) NOT NULL,
SERIE_ALUNO VARCHAR (30) NOT NULL,
DTNASC_ALUNO DATE  NOT NULL,
PERIODO VARCHAR(50) NOT NULL,
CURSO VARCHAR(50) NOT NULL,
RG_RESPON_ALUNO_1 CHAR (11) NULL,
NM_RESPON_ALUNO_1 VARCHAR (40) NULL,
TEL_RESPON_ALUNO_1 VARCHAR(40) NULL,
RG_RESPON_ALUNO_2 CHAR (11) NULL,
NM_RESPON_ALUNO_2 VARCHAR (40) NULL,
TEL_RESPON_ALUNO_2 VARCHAR(40) NULL,
RG_RESPON_ALUNO_3 CHAR (11) NULL,
NM_RESPON_ALUNO_3 VARCHAR (40) NULL,
TEL_RESPON_ALUNO_3 VARCHAR(40) NULL,
RG_RESPON_ALUNO_4 CHAR (11) NULL,
NM_RESPON_ALUNO_4 VARCHAR (40) NULL,
TEL_RESPON_ALUNO_4 VARCHAR(40) NULL,
RG_RESPON_ALUNO_5 CHAR (11) NULL,
NM_RESPON_ALUNO_5 VARCHAR (40) NULL,
TEL_RESPON_ALUNO_5 VARCHAR(40) NULL,

PRIMARY KEY (RM_ALUNO),
)

A partir do campo RM quero preencher as demais informações automaticamente

I’m trying this way but it’s not working.

SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-SEFSOUP\SQLEXPRESS;Initial Catalog=DISPENSA;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("select * from ALUNO where RM_ALUNO=" + txtRm.Text, con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add("@NM_ALUNO", SqlDbType.VarChar).Value = nome;
            cmd.Parameters.Add("@SERIE_ALUNO", SqlDbType.VarChar).Value = serie;

            cmd.Parameters.Add("@PERIODO_ALUNO", SqlDbType.VarChar).Value = periodo;
            cmd.Parameters.Add("@CURSO_ALUNO", SqlDbType.VarChar).Value = curso;

            lblAluno.Text = nome;
            lblSerie.Text = serie;
            lblPeriodo.Text = periodo;
            lblCurso.Text = curso;

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            da.Dispose();
            cmd.Dispose();
            con.Close();

            if (dt.Rows.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
            return ok;

        }
  • 1

    Hello @Leticia. Pf more detail your question. If possible, put the code you have already tried to use to reach your goal.

  • @Leticiacristina, You are already making the connection to the database in your application?

  • Leticia: do not use SELECT * but enter the name of the columns you need to fill the screen; this reduces network traffic and other advantages.

  • I’m using all the information on the screen, I’m not getting the information to pass to the label

  • @Leticiacristina first check if your select is really bringing the data you want

  • Detail, with this da.Fill(dt) this is code to fill the datagrid of c#, never it will appear in a label like this... You don’t even have a datagridview in your form

Show 1 more comment

1 answer

0

Try to use this code:

using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-SEFSOUP\SQLEXPRESS;Initial Catalog=DISPENSA;Integrated Security=True"))
{
    string query = @"SELECT RM_ALUNO,
                            NM_ALUNO,
                            SERIE_ALUNO,
                            PERIODO,
                            CURSO,
                            NM_RESPON_ALUNO_1,
                            RG_RESPON_ALUNO_1
                       FROM ALUNO
                      WHERE RM_ALUNO = @RM_ALUNO";

    using (SqlCommand command = new SqlCommand(query, connection))
    {
        command.Parameters.AddWithValue("@RM_ALUNO", txtRm.Text);

        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                //Nesse caso o retorno possuirá apenas 1 item pois a query filtra pelo identificador
                while (reader.Read())
                {
                    lblAluno.Text = Convert.ToString(reader["NM_ALUNO"]);
                    lblSerie.Text = Convert.ToString(reader["SERIE_ALUNO"]);
                    lblPeriodo.Text = Convert.ToString(reader["PERIODO"]);
                    lblCurso.Text = Convert.ToString(reader["CURSO"]);
                }
            }
            else
            {
                MessageBox.Show(string.Format("O aluno com RM {0} não foi encontrado.", txtRm.Text),
                                "Informação",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Ocorreu um erro ao buscar os dados do aluno de RM {0}, mais detalhes: {1}", txtRm.Text, ex.Message),
                            "Erro",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
        }
    }
}

Browser other questions tagged

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