Community discussion forum

need help with datasets

  • 9 months ago

    hello everybody

    i'm newbie in C#, and i have a big problem that i can't resolve on my own.

    the situation is i have these:


    Code: ( text )
    1. public partial class frmClients : Form
    2.     {
    3.         public SQLiteDataAdapter daClients;
    4.         public DataSet DS=new DataSet();
    5.  
    6.         public int rowID;
    7.        
    8.         ...
    9.  
    10.         // on form_load daClients gets the datas from the database
    11.         // and then get a new dataset, because we don't wanna display all the columns in a DataGrid
    12.         DataSet dsListed=new DataSet();
    13.         SQLiteDataAdapter daC = new SQLiteDataAdapter("select id,companycode as Code, name as Name, deleted as Deleted from clients", daClients.SelectCommand.Connection);
    14.          // popultae the DataGrid with, filtred by the FieldName
    15.          daC.Fill(dsListed,"clients");
    16.          dataGrid1.SetDataBinding(dsListed, "clients");
    17.  
    18.          // I have a method to determinate the selected ID, using the DataGrid1.CurrentRowIndex, and passing to the rowID the "id" column's value
    19.  
    20.          // so until thIs it works everything fine, if  i hit an update button with the following code,
    21.          daClients.Update(DS);
    22.          // it updates the database
    23.          // i understood everything until this point
    24.  
    25.          // i can access the client's data by DS.Tables["clients"].Rows[rowID]
    26.  
    27.      }



    and here comes my big question:
    how can i pass this row to an other form, where the user can change the client's datas, and then pass back to this form's dataset, so i can update it in the database. but it's very very important to do this the least memory allocation.

    can anybody help me with this?

    regards

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 9 months ago

    In the target form..., you must to create an alternate constructor that receives by reference an DataGridRow (or the correct type for the row that you're using) as parameter and assign it into a local member (of same type, of course) of this form.

    Now.. you can to use the local member to manipulate the data.

    All changes that you applies in the referenced object will be applied to the original row (after all.. is the same object Wink ).

    I suggest to you create a class to manage the row's info; let me say: ClientClass. This class must carry out the constructor above mentioned and assign the passed values in the correct member's class. After... you must to pass this class at the alternate constructor of the target form.

     Regards

    Enjoy your coding!!Big Smile

    César

    Mérida, Yuc. Méx.

  • 9 months ago

    thank you for the reply Big Smile

    it was very helpfull.

     

    but i have problems with updating the DB and the initail form's datagrid

     i wrote this:

    Form w = new frmClientDetail(DS.Tables["clients"].Rows[rowID]);
                   
     w.ShowDialog();
    daClients.Update(DS,"clients");

     

    so after closing the target form, i update the DB, but it doesn't work

    also is prefered to update the data in the datagrid

     

    how can i achive that?

    bests

    arnold
     

  • 9 months ago

    Hi mate!!!

     Easy..! check the following code....

    Code in the source form (the form has a linklabel called lnkUpdateClient):

    public partial class frmTest : Form

    {

    //The selected line 

    DataGridViewRow _activerow;public frmTest()

    {

    InitializeComponent();

    }

    private void frmTest_Load(object sender, EventArgs e)

    {

    <color=green>// Some data for test purposes... </color>

    this.dataGridView1.Rows.Add(new object[] {"1", "C‚sar F. Qüeb" });

    this.dataGridView1.Rows.Add(new object[] {"2", "Alejandra Qüeb" });

    }

     

    private void lnkUpdateClient_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

    {

    if (_activerow != null)

    {

    UpdateForm _updateform = new UpdateForm(ref _activerow);

    _updateform.ShowDialog();

    dataGridView1.RefreshEdit();

    }

    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

    {

    _activerow = dataGridView1.Rows[e.RowIndex];

    }

    }

     

    The target form (updater) has the following code:

     public partial class UpdateForm : Form

    {

    DataGridViewRow _passedrow;public UpdateForm()

    {

    InitializeComponent();

    }

    public UpdateForm(ref DataGridViewRow pData)

    {

    InitializeComponent();

    _passedrow = pData;

    txtId.Text = pData.Cells["Id"].Value.ToString();

    txtName.Text = pData.Cells["ClientName"].Value.ToString();

    }

    private void btnReturn_Click(object sender, EventArgs e)

    {

    // you must to update the cells with the content in the textboxes....(are differents object) 

    _passedrow.Cells[
    "ClientName"].Value = txtName.Text;this.Close();

    }

    }

     

    Regards

    César F. Qüeb

    Mérida, Yuc. Méx.

  • 9 months ago

    hello

    first of all thank you for replying

     

    it is really easy, but i can't get it to do the job. i begin ti believe that my DataAdapter.Update() command doesn't do his job.

     

    everything forks fine, i click on a row in a datagrid on the first form, i click a button, appears a new form with the client's details, i change some data, i close the form, i set do the datagrid.SetDataBindings(DS) again, and the datagrid shows the correct datas.

    but,

                    Form w = new frmClientDetail(DS.Tables["clients"].Rows[rowID]);
                    
                    w.ShowDialog();
                    dataGridView1.SetDataBinding(DS, "clients");
                    daClients.Update(DS, "clients");

     i have this code. after the details form closes, i want to update the database too.

     so this line doesn't work daClients.Update(DS, "clients");

     

    i hope someone can help me.

     

    here is the code which initializes the daClients

            private void frmClients_Load(object sender, EventArgs e)
            {

                        // get the connection etc.
     

                        daClients = clsDBUtils.iniAdapter(cmd.Connection, "clients");
                        daClients.Fill(DS,"clients");
                        dataGrid1.SetDataBinding(DS,"clients");

                        // other stuffs
           }

     

    // clsDBUtils 

     

    public static SQLiteDataAdapter iniAdapter(SQLiteConnection conn, string tblName)
            {
                SQLiteCommand selCMD = new SQLiteCommand();
                // swe must initialize the command's connection
                selCMD.Connection = conn;
                selCMD.CommandText = "select * from " + tblName;
                // create the DataAdapter ezecuting the command
                SQLiteDataAdapter da = new SQLiteDataAdapter(selCMD.CommandText, conn);
                da.SelectCommand = selCMD;
                // build automatically the DELETE, INSERT, UPDATE queries, using CommandBulder
                da = buildQueries(conn,da);
                return da;
            }

            //set the DataAdapter's INSERT, UPDATE, DELETE queries
            public static SQLiteDataAdapter buildQueries(SQLiteConnection con, SQLiteDataAdapter da)
            {
                SQLiteCommandBuilder builder = new SQLiteCommandBuilder(da);
                da.UpdateCommand = builder.GetUpdateCommand();
                da.DeleteCommand = builder.GetDeleteCommand(true);
                da.InsertCommand = builder.GetInsertCommand(true);
                return da;
            }

     

    any suggestions?

     

     

  • 9 months ago

    Rare...  i have a question:

    You're using this code when attempt to update:

    w.ShowDialog();
    dataGridView1.SetDataBinding(DS, "clients");
    daClients.Update(DS, "clients");

     

    but.. in the Load event you have:

    dataGrid1.SetDataBinding(DS,"clients");

    this objects are the same?

    Regards

  • 9 months ago

    yes, i have a datagrid wth the name DataGridView1, and i wrote DataGrid1 to not to confuse anybody, but it looks that i succeded Big Smile

     

    anyway i figured it out the mistake.

    i was trying to modify an AUTOINCREMENT UNIQUE primary key Big Smile

    thats why i couldn't update the DB 

     

     thank you guys, i really apreciate your replies

     

    best regards 

Post a reply

Enter your message below

Sign in or Join us (it's free).