views:

91

answers:

3

New to VB.Net,

How to insert or select the dataset value.

    cmd = New SqlCommand("Select * from table1", con)
    ada = New SqlDataAdapter(cmd)
    ds = New DataSet
    ada.Fill(ds)
    cmd = New SqlCommand("Select * from  '" & ds.Tables(0) & "'  ", con)
    mydatatable1.Load(dr3)

It was showing the error in '" & ds.Tables(0) & "', I want to get the dataset value

Need VB.Net Code Help

+1  A: 

You have a reasonable idea right up until you get to the point where you are trying to create a second SqlCommand. That is, once you do the Fill, you already have the data in a table. You wouldn't run another select - you've already done that. You'd just reference the table that you want to use in the dataset.

If you want a data table you would do something like this (for VB, see below):

    SqlDataAdapter myAdapter = new SqlDataAdapter(CommandText, con);
    DataSet myDataset = new DataSet();
    myAdapter.Fill(myDataset, "Table");  // "Table" is just a name, it can be anything.
    mydatatable1 = myDataset.Tables[0];  // Get the table

Now, if you don't need a DataTable, per se, but just want to read from the query, you'd use a SqlDataReader:

 cmd = new SqlCommand(CommandText, con);
 // One or more params
 cmd.Parameters.AddWithValue("@paramName", Value);
 SqlDataReader nwReader = cmd.ExecuteReader();

Then just read from the nwReader:

 while (nwReader.Read())
 {
     string field1Val = (string)nwReader["FieldName"];
     etc...
 }

Update: I don't know VB but here is what I think it would look like:

cmd = New SqlCommand("Select * from table1", con) 
ada = New SqlDataAdapter(cmd) 
ds = New DataSet 
ada.Fill(ds) 
mydatatable1 = ds.Tables(0);

You may well be able to shorten this to get rid of the extra SqlCommand (assuming VB supports this SqlDataAdapater syntax like C#.

ada = New SqlDataAdapter("Select * from table1", con) 
ds = New DataSet 
ada.Fill(ds) 
mydatatable1 = ds.Tables(0);

Good luck...

Mark Brittingham
A: 

you can use Microsoft Enterprise Library for making easy this process. this library is a powerful library for working with datasets.

masoud ramezani
A: 

I think what you are looking for is

cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "' ", con)

cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
cmd = New SqlCommand("Select * from  '" & ds.Tables(0).TableName & "'  ", con)
mydatatable1.Load(dr3)
Sachin
-1 - Why would you ever do this? The second cmd is just a rerun of the first and plays no functional role. Also, even if there *were* some defense, you have an extra set of single quotes.
Mark Brittingham
@Sachin. I used you query, It was showing error as "Incorrect syntax near Table".
Gopal