User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 397,646 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,452 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 645 | Replies: 5
Reply
Join Date: May 2008
Posts: 23
Reputation: fayenot is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
fayenot fayenot is offline Offline
Newbie Poster

batch insert in gridview

  #1  
Jul 7th, 2008
hi....

I have a gridview with rows of info which does not go into edit mode.

A user clicks a checkbox which picks that item and adds a Unit Price

What i need to do is when a user clicks on a button, it should scan through the gridview for all items checked and pick up the Unit Price values and a couple of other field values and insert the information into a sql database

What would be the best way of doing this?

Thanks in advance!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 246
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

Re: batch insert in gridview

  #2  
Jul 7th, 2008
on button click event u jusst check what are the checkboxexs clicked.
u can use this function

  1. submit_click
  2. {
  3. for(int i=0;i<=gridview.rows.count-1;i++)
  4. {
  5. checkbox chk=new checkbox();
  6. chk=(checkbox)gridview.rows[i].findcontrol("chk_name");
  7. if(chk.checked)
  8. {
  9. //write the code needed to get unit prices and insert into database
  10. }
  11.  
  12. }
  13. }
Last edited by peter_budo : Jul 7th, 2008 at 7:00 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: May 2008
Posts: 23
Reputation: fayenot is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
fayenot fayenot is offline Offline
Newbie Poster

Re: batch insert in gridview

  #3  
Jul 7th, 2008
Originally Posted by greeny_1984 View Post
on button click event u jusst check what are the checkboxexs clicked.
u can use this function

submit_click
{
for(int i=0;i<=gridview.rows.count-1;i++)
{
checkbox chk=new checkbox();
chk=(checkbox)gridview.rows[i].findcontrol("chk_name");
if(chk.checked)
{
write the code needed to get unit prices and insert into database
}

}
}


hello thanks for the quick reply...
my problem is how to wirte an SQL statement to insert multiple records.. can you please give me an idea or the code for inserting checked records. i dont konw what code to put in this line
write the code needed to get unit prices and insert into database
}

}
Reply With Quote  
Join Date: Apr 2007
Posts: 246
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

Re: batch insert in gridview

  #4  
Jul 8th, 2008
can u send us u r code.It will help to understand u r query better.
Reply With Quote  
Join Date: May 2008
Posts: 23
Reputation: fayenot is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
fayenot fayenot is offline Offline
Newbie Poster

Re: batch insert in gridview

  #5  
Jul 8th, 2008
 protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox chkbox = (CheckBox)row.FindControl("chkboxSelect");
            TextBox UnitPrice = (TextBox)row.FindControl("txtUnitPrice");

            if (chkbox.Checked == true)
            {
                try
                {
                    conn.Open();

                    int Supp = Convert.ToInt16(Request.QueryString["SupplierId"]);
                    SqlCommand cmd = new SqlCommand ("INSERT INTO SuppMat (SupplierId, MaterialID, UnitPrice) VALUES (@SupplierId,'" + row.Cells[1].Text + "','" + UnitPrice.Text + "')", conn);
                    cmd.Parameters.AddWithValue("@SupplierId", Supp);
                   
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch
                {
                    Response.Write("");
                }
                finally
                {
                    conn.Close();
                }
            }
           
        }
        
    }

here's my code..my only problem now is that it does not save to the database.. hope you can help me..
Reply With Quote  
Join Date: Apr 2007
Posts: 246
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

Re: batch insert in gridview

  #6  
Jul 8th, 2008
r u getting values in row.cells[i].text.did u test u r query in query analyser ,r there any problems.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 10:59 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC