data in grid

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

I'm reading information from store procedure and I would like to put the
result in dataGrid.
In all examples and books about working with store procedures is the example
only with console.
I'm using windows form.

My code:

void SearchSastojci(string sastojci)

{ SqlConnection conn = null;

SqlDataReader rdr = null;

ReceptiDb.RcptDb db = null;

string sConnectionString = "packet size=4096;user id=sa;persist security
info=False;initial catalog=Recepti";

try

{

conn = new

SqlConnection(sConnectionString);

conn.Open() ;

db = new RcptDb(sConnectionString) ;

SqlCommand cmd = new SqlCommand(

"Names", conn);

// 2. set the command object so it knows

// to execute a stored procedure

cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which

// will be passed to the stored procedure

cmd.Parameters.Add(

new SqlParameter("@Group", Group.Text ));

cmd.Parameters.Add(

new SqlParameter("@Name", textBoxName.Text ) );

// execute the command

rdr = cmd.ExecuteReader();

// iterate through results, printing each to console

while (rdr.Read())

{

grid.AddColumn("Name", "Name") ;

grid.AddColumn("Group", "Group") ;

----- Here I need to read rdr into grid ----- ???





}

}

finally

{

if (conn != null)

{

conn.Close();

}

if (rdr != null)

{

rdr.Close();

}

}

}


Hrcko
 
Hrcko,

I assume you are using a webform datagrid, because that is the only one that
you can connect(fill in this case) using datasource and the reader. It needs
than after setting the datasource a datagrid1.databind as next instruction.

When it is a windowform than you can create a dataset/that as datasource,
which you connect to your dataset/datatable using the datagrid.datasource,
here is not the datagrid.databind

It is not important if you use a storedprocedure or a simple text procedure
for your sqlcommand object for that.

I hope this gives some ideas

Cor
 
Back
Top