Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

datagrid updating c# asp.net

Options
  • 07-02-2006 6:14pm
    #1
    Registered Users Posts: 742 ✭✭✭


    trying to update the database using the update button column on datagrid

    im using oledbdatadapter and dataset to populate it, how do i pull values off it before it goes into edit mode or how do i use the dataset.update method to update the db

    thanks


    private void update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    tb = (TextBox) e.Item.Cells[0].Controls[0];
    FlightID = tb.Text;
    tb = (TextBox) e.Item.Cells[1].Controls[0];
    Destination = tb.Text;
    tb = (TextBox) e.Item.Cells[2].Controls[0];
    Date = tb.Text;
    tb = (TextBox) e.Item.Cells[3].Controls[0];
    Status = tb.Text;
    tb = (TextBox) e.Item.Cells[4].Controls[0];
    Gate = tb.Text;
    tb = (TextBox) e.Item.Cells[5].Controls[0];
    PassengerNumber = Int32.Parse(tb.Text);





    string sql = "Update FlightDeparture Set FlightID = '" + FlightID + "', Destination = '" + Destination + "', [DateTime] = #" + DateTime.Parse(Date) + "#, Status = '" + Status + "', Gate = '" + Gate + "', PassengersTotal = " + PassengerNumber + " Where FlightID = '" + fID + "'";


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I'm not sure what exactly you're asking but you could put something like below into the edit event:
    private void dgFoo_Edit(object source,
         System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
                            
    			dgFoo.EditItemIndex = e.Item.ItemIndex;
                            // i below equals the required index and would be 
                            // populated in real code (you could use a literal)
    			string editValue = e.Item.Cells[i].Text; 
    			dataBind();
    
    		}
    
    When you're not in edit mode you can access a TableCell's Text property directly, you don't need to cast it as a textbox first. It doesn't go into edit mode until after the edit event has finished executing.

    If you want to read a Cell's content from somewhere else in code then the following should work:
    private string foo(int itemIndex, int cellIndex)
    		{
    			return dgFoo.Items[itemIndex].Cells[cellIndex].Text;
    		}
    

    It's two o'clock in the morning :( and I think I've oversimplified so I'm going to bed - I'll look at it again tomorrow if nobody else has answered.

    [edit]
    The datasource you're binding to (I'm assuming it's a dataset) doesn't persist unless you declare it at class level instead of local level (which I'm not recommending) nor does it persist within the DataGrid's DataSource property. Hence the need to access the TableCells directly instead of using DataSource. And that's why you can't use DataSet.Update() - but I could be wrong, as I've said it's 2am, 2:30am now.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Do a google on SQL Injection as well before rsynnott kills you :v:


  • Registered Users Posts: 742 ✭✭✭Loco


    thanks. tried the edit part but its still not taking a value from the grid :(


  • Registered Users Posts: 742 ✭✭✭Loco


    works fine now using session variable for it


Advertisement