Results 1 to 15 of 15

Thread: call insert through gridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    call insert through gridview

    wits end im hoping to find solution from you guys.

    i have a gridview with a few columns, 2 textbox templatefield,1 button template and bound to sqldatasource .

    the button template will call the procedure insert_item with parameter (itemid, rcvd_qty, rate).

    how can i hook up the insert_item(x,y,z) to the gridview button?

    design sample
    Learn something new every .001 second.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Assuming you have added an InsertCommand to you SqlDataSource, then all you need to do is set the CommandName property of your button.

    Have a look at this article:

    http://www.aspdotnetfaq.com/Faq/How-...ataSource.aspx

    Hope that helps!

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    @gep13 thanks it seems what im looking.

    also i want to add validation on addbutton_click through javascript. i need to meet the conditions

    1] Rcvd_Bal <=Bal of the active row
    2] Rcvd_Rate <=PO Rate of the active row
    Learn something new every .001 second.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Hello,

    In the TemplateField, where you have the TextBoxes, you can also had some Validator Controls. In your case, you won't be able to use an Out of the Box validator, but you could use a CustomValidator.

    Gary

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    great. thanks for the info...let me take this one at a time.
    Learn something new every .001 second.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Sure, just let me know if you have any follow up questions.

    Gary

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    twas a kick ass sample . the insert button works on fly but im getting error when using the value pass from gridview textbox.


    Code:
    <asp:TemplateField HeaderText="rcvd qty">
    <ItemTemplate>
    <asp:TextBox runat ="server" ID="rcvdqty_textbox" Text="0" Width="50"></asp:TextBox>
     </ItemTemplate>
    </asp:TemplateField>

    vb Code:
    1. Dim rcvdqty As TextBox = TryCast(GridView2.Controls(0).Controls(0).FindControl("rcvdqty_textbox"), TextBox)
    2. Dim _rcvdqty As New SqlParameter("@rcvdqty", SqlDbType.Float)
    3.  
    4. _rcvdqty.Direction = ParameterDirection.Input
    5.  
    6. 'this line doesnt work and it returns an error
    7. 'Object reference not set to an instance of an object.
    8.  
    9.  _rcvdqty.Value = rcvdqty.Text  
    10.  
    11. insertParameters.Add(_rcvdqty)

    heres the grid.
    Last edited by jlbantang; May 19th, 2011 at 08:27 AM.
    Learn something new every .001 second.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Hello,

    If you set a break point on this line:

    Code:
    _rcvdqty.Value = rcvdqty.Text
    Can you confirm that rcvdqty is null/Nothing?

    Gary

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    hi gary, yes its says nothing.

    so this code did not work i assume.
    Code:
    Dim rcvdqty As TextBox = TryCast(GridView2.Controls(0).Controls(0).FindControl("rcvdqty_textbox"), TextBox)
    ...still finding sample

    from the post#7 screenshot i need to add data under columns rcvdID, grfID, rcvd qty and rcvd rate in the insert command but im not getting the correct row index.
    Last edited by jlbantang; May 21st, 2011 at 06:09 AM.
    Learn something new every .001 second.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Hey,

    Correct, that line is the problem one.

    If FindControl does not find the item that you are looking for, it will return a null/Nothing.

    If I were you, I would set a break point on that line, and then start your debugging. i..e what control do you get back when you do this:

    Code:
    GridView2.Controls(0)
    Begin stepping into the code, using the watch window, and I am sure you will figure out what is going on.

    Gary

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    hey gary, i manage to find a time saver solution from different threads. please let me know if their is a need to revise the code.

    vb Code:
    1. If e.CommandName = "insert" Then
    2.  
    3.  'get fired button from gridview container and return as row
    4.  Dim rowSelect As GridViewRow = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow)
    5.  
    6.  'get row index
    7.  Dim rowindex As Integer = rowSelect.RowIndex
    8.  
    9. 'get textbox value using rowindex  at col 6
    10.  Dim txt As TextBox = DirectCast(GridView2.Rows(rowindex).Cells(6).FindControl("rcvdqty_textbox"), TextBox)
    11.  
    12. 'do the rest
    13.  
    14. End if

    thanks.
    Learn something new every .001 second.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    great now i need to perform client validation. 3 rules:

    1. rcvd qty is a required field. can be done through required field validation control
    2. rcvd qty must be numeric-float in format. can be done through regular expression validation control
    3. now comes another issue. i need to compare the rcvd qty with balance qty. its obvious rcvd qty should be always <= balance qty. i used label control for balance qty and textbox for rcvd quantity. having said that it seems the comparison validation control do not support textbox and label comparison. now the work around again to use javascript. simple and easy , call javascript validation function when insert button fires up.

    im just wondering why im not getting the value of rcvd textbox during grid iteration. in the page source the rcvd qty has this name GridView3$ctl03$rcvdqty_textbox where ctl03 is the control index. similarly the balance label has this name series GridView3_ctl02_balanceLabel where ctl02 is the control index.

    somehow im not getting the value of rcvdqty textbox.
    Code:
    var thetextbox;
    thetextbox=document.getElementById('GridView3_ctl02_rcvdqty_textbox');
    alert(thetextbox.innerHTML);
    suggestions are open. thanks.
    Last edited by jlbantang; May 22nd, 2011 at 08:10 AM.
    Learn something new every .001 second.

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Hello,

    Change you code slightly to be this:

    Code:
    alert(thetextbox);
    What do you get?

    Gary

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: call insert through gridview

    hi gary,

    got it with this code
    thetextbox=document.getElementById('GridView3_ctl02_rcvdqty_textbox').value;
    alert(thetextbox);

    im doing the other stuff. i'll be back quickly. thanks.
    Learn something new every .001 second.

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: call insert through gridview

    Cool, looks like you have it.

    The reason that I was asking you to do this:

    Code:
    alert(thetextbox);
    Was to prove that the getElementById was actually finding something. If it wasn't you would have got a null in the alert message. You should have been getting object, the extension from that would have been to again test:

    Code:
    alert(thetextbox.innerHTML);
    If this returned null, then the natural conclusion would have been that the innerHTML property was not available.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width