Jump to content

Preencher uma GridView com dados do SQL


whize13
 Share

Recommended Posts

Classe.ligacao.Open()
    Try
	    Dim queryCodDisciplina As New SqlCommand("SELECT CodDis FROM Disciplinas WHERE DesignacaoDis ='" & DDLDisciplinas.Text & "'", Classe.ligacao)
	    Dim CodDis As Integer = queryCodDisciplina.ExecuteScalar

	    Dim ad As New SqlDataAdapter
	    ad.SelectCommand = New SqlCommand("SELECT Licao, Sumario, DataSumario, DataRegisto FROM RegistoDia WHERE CodProf ='" & Session("CodProf").ToString & "' and CodDis = '" & CodDis & "' and IDTurma ='" & Session("IDTurma").ToString & "'", Classe.ligacao)
	    Dim ds As New DataSet
	    ad.Fill(ds)
	    GVSumario.DataSource = ds
	    GVSumario.DataBind()

	    Classe.ligacao.Close()

    Catch ex As Exception
	    MsgBox(ex.Message)
    End Try
    Panel1.Visible = True
    GVSumario.Visible = True

Pronto, eu exprimentei o codigo e ficou assim o problema é a GridView não está visivel

Edited by whize13
Link to comment
Share on other sites

Não sei se ainda precisas de ajuda, mas eu também não consegui fazer com este método.

Em SQL Server, se usar só um DataReader, fica operacional, com OracleDB, não chega.

Deixo aqui dois exemplos que funcionam bem para mim.

SQL Server

using (SqlConnection conn = new SqlConnection(connectionString))
	    {
		    string query = @"select ID, first_name, last_name, age, class from Persons with (NOLOCK) where sex = "+auditNumber+@";
					 select * from Department where name='open';
					 select * from Persons with(NOLOCK) inner join Jobs
				    on Persons.work = Jobs.work_id
				    where sex = "+auditNumber+@"";
		    SqlCommand command = new SqlCommand(query, conn);
		    SqlDataReader reader = null;
		    command.CommandType = CommandType.StoredProcedure;
		    command.Parameters.AddWithValue("sex", SqlDbType.Bit).Value = auditNumber;
		    try
		    {
			    if (conn.State == ConnectionState.Closed)
			    {
				    conn.Open();
			    }
			    reader = command.ExecuteReader();
			    //para cada grid aloca datasource e faz databind.
			    GridView1.DataSource = reader;
			    GridView1.DataBind();
			    //passa ao proximo select
reader.NextResult();
			    GridView2.DataSource = reader;
			    GridView2.DataBind();
			    mainView.SetActiveView(boxView);
			    lblError.Text = "Concluída a extração de informação da Base de Dados. Timestamp: " + DateTime.Now;
		    }
		    catch (SqlException ex)
		    {
			    lblError.Text = "Erro de SQL: " + ex.ToString();
		    }
		    finally
		    {
			    conn.Close();
		    }
	    }

OracleDB


		    OracleCommand command = new OracleCommand(query,conn);
		    OracleCommand command2 = null;

		    OracleDataReader reader = null;
		    DataTable dt = new DataTable();


		    command.CommandType = CommandType.Text;
		    try
		    {
				    conn.Open();
			    reader = command.ExecuteReader();
				 dt.Load(reader);
			    GridView1.DataSource = dt;
			    GridView1.DataBind(); 

			    reader.Close();
			    conn.Close();
		    }
		    catch (OracleException ex)
		    {
			    lblError.Text = "Erro de OracleSQL: " + ex.ToString();
		    }

No caso de OracleDB vi-me obrigado a fazer uma DataTable por cada grid que queria preencher, ainda não descobri porquê. Mas a verdade é que isto funciona, apesar de ser à lei do martelo.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.