none
How to add a row to dataset binded to txtbox controls and finally calling dataadapter.update?? RRS feed

  • Question

  • Hi all,
    I am developing a data based app, with sqlserver as dataprovider... i have stuck up in a situation where i am not sure to follow which way to go. pls help me after understanding the scenario....

    i got 1 untyped dataset , 1 dataadapter, 1 binding source, and many text boxes as to display rec and insert a new record. when form loads dataadapter fills empty dataset ( in  app 1 rec at a time is retrieved ) . then databinding through  bindingsource to textbox controls is done at runtime.
    now i can retreive a rec based on client id i.e ( "select * from clientprofile WHERE ClientID=@ClientID) and parameter is passed when dataadapter.select  is called.

    The actual part i have stuck with is i dont know how to insert a record to dataset through databinded textboxes without providing clientID coz clientID is autoincrementing it has to return clientid from database after insertcommand executed. But the commandbuilder has built in such way that all the fields are parameterised, thats ok but how to link them to textboxes with the help of bindingsource. if not i should manually put some lines linking txtbox.text to parameters.

    SO please help me with logic...
    if i got the logic i can draw the codes....
    Thursday, November 12, 2009 4:48 PM

Answers

  • The actual part i have stuck with is i dont know how to insert a record to dataset through databinded textboxes without providing clientID coz clientID is autoincrementing it has to return clientid from database after insertcommand executed. But the commandbuilder has built in such way that all the fields are parameterised, thats ok but how to link them to textboxes with the help of bindingsource. if not i should manually put some lines linking txtbox.text to parameters.

    SO please help me with logic...
    if i got the logic i can draw the codes....

    When using DataSets/DataTables to databind to UI controls, I have typically used the actual DataTable rather than a BindingSource for databinding, although that probably doesn't matter at all.

    You should go ahead and add a row to your DataTable by either using the BindingSource.AddNew() method or the MyDataTable.Rows.Add(MyDataTable.NewRow()) method.

    As far as your auto-incrementing ClientID goes, that is only a problem if you are allowing the user to enter that for a new Client record. Normally, Primary Key fields of this type shouldn't be user-generated.

    If you need the user to know what the Client ID is, what you could do is create the new row (as I showed with the .AddNew or .Add methods), then .Update() the database and get the new Client ID ... which you would then display in the ClientID textbox (or however you're doing that). Then when the user finishes filling out the rest of the form you'd .Update() again. Of course, the problem with this is that the record gets added to the database, without a chance of the user cancelling the addition.

    I don't know how much this answer helps you. Let me know if you have more questions. I have several DataAccess entries in my blog if you want to take a look.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 2:25 AM
    Thursday, November 12, 2009 5:46 PM