8 Replies - 19921 Views - Last Post: 13 March 2011 - 12:17 PM Rate Topic: -----

#1 chuck6969ya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 11

Pulling Data from Gridview and Insert to Database

Posted 12 March 2011 - 09:39 PM

Hi there, very much a beginner here. I have been most successful with Visual Studio when I am able to stick to TableAdapters .. Anyhow, I have a project I am woking on - I Enter into a User Table a New User. then I use a DDL to poll Active users - then that DDL's Postback triggers the Gridview to fill. I am trying to GET the data outof the Gridview that is created - and INSERT it into the SQL Database.

The approach that I am figuring is the best route - is to Loop over the Gridview and then get that Data into a DataTable that is ceated in the .CS file.

I would paste code - but it's essentially incomplete, and does not work. I lack horribly @ writing the code behind stuff. I have spent likely 12 hours battling with this. If anyone has a link to something that is a working example - it's most apreciated.

HELP !

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Pulling Data from Gridview and Insert to Database

#2 ice-t  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 104
  • Joined: 26-May 09

Re: Pulling Data from Gridview and Insert to Database

Posted 12 March 2011 - 11:49 PM

for (int i=0 i < DataGridView1.Rows.Count;i++){
}


and then insert data into tableName with values from the gridview DataGridView1.Item(0,i).value , DataGridView1.Item(1,i) .. etc.
i is the row and 0,1 is the columns, this is for 2 columns, u can get the columns too with column index (and make another loop)
Was This Post Helpful? 1
  • +
  • -

#3 chuck6969ya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 11

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 12:04 AM

Thanks for the fast reply - and that looks like what I need.
( I assume that you agree that my approach for this is the "best" way ... )
Question - do I need to define / use any Variables to accomplish my task ... essentially I am trying to do a SQL "Batch" Insert .. .? My term's may be off a bit - as mentioned before --- I am VERY beginner ... Can you provide any syntax for a Batch Insert ? ( if it's even called batch ... )

I am trying to define a 2 Column Data Table - that will accept AND INSERT - anywhere from 3 to 8 rows ( data from Gridview ) and INSERT into a Table. IT's a "new hires To Do Tasks website", so I first CREATE the user ( gathering StartDate and name etc .... ) THEN I have been totally stuck on how to INSERT the needed lines - into my "Data" Table ... ( built of UserID, TaskID, IsComplete, Date Completed, etc .... ) The Gridview Fills with the "Tasks" ( 3 to 8 rows depending on Department ) --- then I need to gather and Insert into my Data Table ... the rst I figure would be easy - this one STEP is the real killer ... !
Was This Post Helpful? 0
  • +
  • -

#4 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 01:05 AM

What you want is a Parametrized SQL Query

These following links will get you on you're way.

SQL Insert with Parameters
http://idealprogramm...sert-statement/

Learn more about SQL Parameters


Here is an UPDATE Statement example

using (SqlCommand sqlcommand = new SqlCommand("Update[" + txtDBNameSql.Text + "].[dbo].[Item] Set [Description]=@Description,[Price1]=@Price1,[Notes]=@Notes WHERE [Code]=@Code", sqlconnection))
                    {
                        sqlcommand.Parameters.AddWithValue("@Code", Code);
                        sqlcommand.Parameters.AddWithValue("@Description", Description);
                        sqlcommand.Parameters.AddWithValue("@Price1", Price1);
                        sqlcommand.Parameters.AddWithValue("@Notes", notes);
                        lstBoxRooms.Items.Add("Updated room: " + Code);
                        sqlcommand.ExecuteNonQuery();

                    }




Greetings
Marinus
Was This Post Helpful? 0
  • +
  • -

#5 chuck6969ya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 11

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 10:10 AM

I atart writing it and get lost - little Red underlines appear - then I try to fix and screw it all up. With the below - it complains that the "dr" is never used ... which is true I bet .. and when I typed in the "i" in the For statement over the GridView rows -- it's telling me it wants a ;

Please help with Firm "steps" definition. Like:
#1 - Declare DataTable
#2 - Declare Rows
#3 - Declare Variables
#4 - somehow get the Data from Gridview1 Cells - INTO the Variables
#5 - Form SQL INSERT statement - using said Variables -
#6 - Loop for another row ...

I did paste the crap I have come up with - which is likely now a collage of 2 different approaches ... I guess the fact that there are proably 15 different ways to "do" what I am trying to do - does NOT help !!! ;)

protected void btnGetData_Click(object sender, EventArgs e)
    {   
    // CREATING NEW DATATABLE & COLUMNS
    //DataTable dt = new DataTable();
    // DataRow dr;
    // dt.Columns.Add("UserID", typeof(Int32));
    // dt.Columns.Add("TaskID", typeof(Int32));
    string UserID=("GridView1.Item(0,i).value");
    string TaskID=("Gridview1.Item(1,i).value");
    // for (int i=0 i < (GridView1.Rows.Count;i++);
    {
    // dr = ("UserID","TaskID");
    // InsertNewDataRow;
    using (SqlCommand Sqlcommand = new SqlCommand("INSERT["
    
    
    }



THANKS - sorry I am such a crapper - but I am TOTALLY new - and essentially have not coded anything other than Batch files ... I think I have good "logic" but that would be my ONLY asset !
Was This Post Helpful? 0
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 10:32 AM

Hi , this code is all wrong :)

this line
for (int i=0 i < (GridView1.Rows.Count;i++);


should be

for(int i=0; i < GV.Rows.Count; i++){
}


And DataRow is never defined , it you should Declare it

DataRow row = new DataRow();

What is this .

string UserID=("GridView1.Item(0,i).value");


That is not a way to get data at all :) the GridView1 Doesn't support Item

It should be more in the lines of
 string UserID= GridView1.Rows[x].Cells[3].Text;



Clearly you need to learn alot more about C# even before touching the .NET framework goodies .

A book i recommend is C# Cookbook 3.


Here is also Data Access tuts

http://www.asp.net/d...ccess/tutorials

GridView Structure
http://msdn.microsof...s.gridview.aspx

Good Luck


Greetings

Marinus

This post has been edited by marinus: 13 March 2011 - 10:37 AM

Was This Post Helpful? 1
  • +
  • -

#7 chuck6969ya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 11

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 11:47 AM

Thanks for that - Yes - I am downloading that book now. I appreciate your comments and code exapmples..

Can you confirm the overall actions I need - If I went with your examples ( most recent ) - then my "steps" would be :

1 Define the Variables ( as you showed )
2 Incorporate them into the loop For Each statement ...
3 Create an Insert SQL statement to perform the Insert

??? is this in the ball park ?

Thanks Much
Was This Post Helpful? 0
  • +
  • -

#8 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 12:13 PM

Depends if you want to loop through a GridView i would recommend using the for loop , but other than that you pretty much got the idea ;)


Greetings
Was This Post Helpful? 0
  • +
  • -

#9 chuck6969ya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 11

Re: Pulling Data from Gridview and Insert to Database

Posted 13 March 2011 - 12:17 PM

Cool - Thanks for your help - I will hopefully get'er done ! I will let you know how goes - or possibly ask a question that is "real" !

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1