You must Sign In to post a response.
  • Category: .NET

    How to get id from database for update query in c#(windows application)

    Friends..i want to update customer table from customer name inserted in Textbox..
    but.see there is no textbox for customer id..I would like to take that id from database and update customer name in database for that id..

    how should i complete this query???

    sSql = "Update company set CNAME='" + custnm.Text + "' where cid=..............;

    Help..me Plz..
  • #715261
    You can change this to as


    sSql = "Update company set CNAME='" + custnm.Text + "' where cid = (Select Id from compnay where CNAme '" + custnm.Text + "' )

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #715270
    In first you have the old name of customer and keep it in a hiddenfield.. at the time of updation you can compare the name and select the id


    sSql = "Update company set CNAME='" + custnm.Text + "' where cid = (Select Id from compnay where CNAme '" + hdnfiels.value+ "' )

    Thanks,
    Sini
    "Always Ready to accept challenges....."

  • #715271
    hi Sini..

    doesn't work..

    hdnfiels ??????

    error/// The name 'hdnfiels' does not exist in the corrrent context

  • #715278
    hi tushar
    first you have to remember old value of that particular user name because in table which row you want to update at runtime we can not find it so we have to take old value from table and then fire update query you can use DataGridview and then Gridview's selected index changed event you can write to store old value to any label or any where else and then get old value and then fire update query like

    sSql = "Update company set CNAME='" + custnm.Text + "' where cid = (Select Id from compnay where CNAme '" + lblname.text+ "' )


    On gridview's selected index changed you have to store its id or text on label and then fire update query.



    Hope this will help you,

  • #715281
    Thanks..jignesh
    ur Solution works..

  • #715309
    try below query:

    string query = "Update company set CNAME='" + custnm.Text + "' where cid = (Select Id from company where CNAME '" + lblname.text+ "' )

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.