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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

datagrid updating c# asp.net

  • 07-02-2006 5:14pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


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


  • Registered Users, Registered Users 2 Posts: 742 ✭✭✭Loco


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


  • Registered Users, Registered Users 2 Posts: 742 ✭✭✭Loco


    works fine now using session variable for it


Advertisement