Datasets - Urgent!!@

G

Guest

I have the follow code:

private void crear() {

//creating a dataset

DataSet myDataset=null;
SqlConnection sqlConnection=null;
SqlDataAdapter sqlCommand=null;
try {
//create an instance for a SqlConnection and a Sql Command

sqlConnection = new SqlConnection("server=.; database=JF;
Trusted_Connection=false; uid=sa; pwd=; Pooling=true;");
sqlCommand = new SqlDataAdapter("select
cantidad,concepto,prec_unit,valor_venta from fact_detalle",sqlConnection);
myDataset = new DataSet();

//mark the command as a text
sqlCommand.SelectCommand.CommandType = CommandType.Text;

//open the connection and execute
sqlConnection.Open();
sqlCommand.Fill(myDataset);

}
catch {
throw;
}
finally {
if(sqlConnection!=null)
sqlConnection.Close();
// MessageBox.Show("The connection is close");
}
// return myDataset;

dataGrid1.DataSource=myDataset.Tables[0];
}


private void nueva_fila()

{
DataRow anyRow = myDataset.Tables[0].NewRow();
}


---------------------------------
on nueva_fila there´s an error , when compila the "myDataset" is underlined
and the follow task is shown:

The type or namespace name 'myDataset' could not be found (are you missing a
using directive or an assembly reference?)
 
C

Cor Ligthert [MVP]

Alex,

First of all you need in my opinion only 50% of the code you use at the
moment.

It is complete nonsense to create an object in a try catch block, because
that cannot throw an exception. The same is for all others, it is only
needed for your method dataadapter.fill (because that it has an inbuild open
and close, is even the open and close not needed). For your Try catch finaly
you can use a Using block.

At last your error, that is because the dataset is created in the method
"crear", therefore you have no direct reference any more to it although that
it exist because it has at least a reference to the DataGrid.

You can get it in the method nuava_fila in a way as
DataTable myDataTable = (DataTable)Datagrid1.DataSource

Or you can declare your mydataset global in your class.

I hope that this gives some idea's

Cor
private void crear() {
SqlConnection sqlConnection = new SqlConnection("server=.; database=JF;
Trusted_Connection=false; uid=sa; pwd=; Pooling=true;");
sqlCommand = new SqlDataAdapter("select
cantidad,concepto,prec_unit,valor_venta from fact_detalle",sqlConnection);
myDataset = new DataSet();

//mark the command as a text
sqlCommand.SelectCommand.CommandType = CommandType.Text;

//open the connection and execute
sqlConnection.Open();
sqlCommand.Fill(myDataset);

}
catch {
throw;
}
finally {
if(sqlConnection!=null)
sqlConnection.Close();
// MessageBox.Show("The connection is close");
}
// return myDataset;

dataGrid1.DataSource=myDataset.Tables[0];
}


private void nueva_fila()

{
DataRow anyRow = myDataset.Tables[0].NewRow();
}


---------------------------------
on nueva_fila there´s an error , when compila the "myDataset" is
underlined
and the follow task is shown:

The type or namespace name 'myDataset' could not be found (are you missing
a
using directive or an assembly reference?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top