Solved

pulling data from a detailsview

Posted on 2009-03-31
9
437 Views
Last Modified: 2013-11-07
ASP.NET 2.0/VB.NET 2.0 and sql server 05: I have a form that when the user chooses a "unit no" from a drop down list it fills several textboxes on a form. Being that I'm using a sqldatasource I have to do this with a detailsview (or formview). I read the only way to do this. Well, the data that's been populated in these two detailviews needs to be saved to a different database. These detailsview fields are part of a "unit record". I need to know how to pull the data from these detailviews. Each dvonly has one label in it. How is this done?
Am I close???

 

dvWellName.Rows(0).Cells(0).Text  

 

Used like: command.Parameters.Add("@WellName", SqlDbType.VarChar).Value = dvWellName.Rows(0).Cells(0).Text

Open in new window

0
Comment
Question by:techpr0
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:JenniQ
ID: 24033322
You are pretty close.

Once you populate the dataview it becomes an object you can access.

In your code behind you can access the contents like this:

DataSet ds;
ds.Fill(dvWellName);

ForEach(DataRow dr in ds) {
 // here you can do stuff
    dr["myCOlumnName"];
}

or maybe you coudl do this:

For(int i = 0; i<dvWellName.Items.Count; i++) {
  command.Parameters.Add(dr[i], SqlDbType.VarChar).Value = dr[i]);


At any rate, the solution lies in creating a DATAROW and  DATACOLUMNs, filling them with the contenst of your dataview, and maniuplating. Here is a better code sample:

private void CreateNewDataRow()
{
    // Use the MakeTable function below to create a new table.
    DataTable table;
    table = MakeNamesTable();

    // Once a table has been created, use the
    // NewRow to create a DataRow.
    DataRow row;
    row = table.NewRow();

    // Then add the new row to the collection.
    row["fName"] = "John";
    row["lName"] = "Smith";
    table.Rows.Add(row);

    foreach(DataColumn column in table.Columns)
        Console.WriteLine(column.ColumnName);
    dataGrid1.DataSource=table;
}

private DataTable MakeNamesTable()
{
    // Create a new DataTable titled 'Names.'
    DataTable namesTable = new DataTable("Names");

    // Add three column objects to the table.
    DataColumn idColumn = new  DataColumn();
    idColumn.DataType = System.Type.GetType("System.Int32");
    idColumn.ColumnName = "id";
    idColumn.AutoIncrement = true;
    namesTable.Columns.Add(idColumn);

    DataColumn fNameColumn = new DataColumn();
    fNameColumn.DataType = System.Type.GetType("System.String");
    fNameColumn.ColumnName = "Fname";
    fNameColumn.DefaultValue = "Fname";
    namesTable.Columns.Add(fNameColumn);

    DataColumn lNameColumn = new DataColumn();
    lNameColumn.DataType = System.Type.GetType("System.String");
    lNameColumn.ColumnName = "LName";
    namesTable.Columns.Add(lNameColumn);

    // Create an array for DataColumn objects.
    DataColumn [] keys = new DataColumn [1];
    keys[0] = idColumn;
    namesTable.PrimaryKey = keys;

    // Return the new DataTable.
    return namesTable;
}


-- Jenni

For a better code sample, sen
0
 

Author Comment

by:techpr0
ID: 24033953
Jen,
Thanks for your effort but I'm a bit confused. Are you sure you're explaining how to pull a value from a Details View? I don't see a DetailsView listed any where in your explaination. If I'm wrong I apologize but it's just not clear. I have a DetailsView with one label (template field) that gets populated by a sqldatasource that I need to pull the/a value from to save else where. Please help.
0
 
LVL 3

Expert Comment

by:JenniQ
ID: 24034006
No problem.

What I'm trying to have you do is dump the content of your DetailsView control into a DataTable. That is what the dt.Fill(DetailsView) does.

Onces its in the DataTable, there is a lot more options availble to you.

Hope that makes sense.

0
 

Author Comment

by:techpr0
ID: 24035087
Ok, yes. Now I do understand. Here's my vb.net version with the parameter.add code added:
Dim ds As DataSet
ds.Fill(dvWellName)

For Eachdr as DataRow dr in ds
          ' here you can do stuff
          command.Parameters.Add("@WellName", SqlDbType.VarChar).Value = dr("Well Name")
Next

When I run I get the error:
Error      1      C:\Inetpub\wwwroot\...\UnitMF.aspx.vb(82): error BC30456: 'Fill' is not a member of 'System.Data.DataSet'.

Is .Fill only a c# function? What can I do to make this work. Your help is appreciated.            
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 13

Expert Comment

by:drypz
ID: 24036300
Hi! DataAdapter(SqlClient/OleDb) has the Fill() method not Dataset.
0
 
LVL 3

Accepted Solution

by:
JenniQ earned 500 total points
ID: 24040571
Sorry its not DataSet its System.Data.DataAdapter. Here is a code sample:

Dim custAdapter As DataAdapter = New DataAdapter()

' add the control's values to the DataAdapter here

Dim dsWellName As DataSet = New DataSet()
custAdapter.Fill(ds)

-- jq
0
 

Author Comment

by:techpr0
ID: 24043784
Sorry to be a PITA but could you maybe give me an example of adding the control's values to the DataAdapter.
0
 
LVL 3

Expert Comment

by:JenniQ
ID: 24043949
techpr0 - I would never call you a PITA :P

You can get the text in each cell of the row by

dvWellName.Rows[i].Cells[j].Text
OR IN VB
dvWellName.Rows(i).Cells(j).Text

So, you could like loop through each row in the dvWellName control, and add it to a datarow in a dataset, then you have this nice dataset that you can do oh so much fun stuff with. YAY!

See how Rows[] is an array? That should be the key to getting what you need...

...erm... what was it again that you were trying to do?

-- jenni
0
 

Author Comment

by:techpr0
ID: 24044506
thanks jenni, really i was just wondering about the syntax of adding the control's values to the DataAdapter. but wheather you get back with me or not you certainly earned these points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 44
Error on Add method 1 38
Adware on IIS hosted asp.net website 1 20
C#.NET and microsoft certification. 3 34
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now