One form for five tables

rHasan

Active member
Joined
Jan 10, 2007
Messages
43
Programming Experience
3-5
I've five table called Buyer, Order, Fabrics, Color, Size which all have two fields of the same data type called ID & Description, like ColorID, ColorDescription etc.

I've a form which have a data grid and two text box for display and modifying data. I've some buttons called Add, Update, Delete, Save, Cancel to provide these functionality.

Can I use this form for displaying and modifying all the tables ? How ?

I want something like creating a Property where a table name is given while loading the form, and work with that table. Is it possible?

Thnk you for your helps!
 
yes it is possible but it maens that when you load your form, you need to manually set up the databinding for each control.

Have a bindingsource for each of the tables, and then when it loads, set the controls to use the appropriate bindingsource.
 
naww.. it can all be done in the designer. read the dw2 link in my signature about displaying related data
 
Thnx guys for your help. But I've found out a solution for this myself. I've created a property for the form called Table, and supplied a table name to the form to work with while loading. Here is the solution -

VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data.OleDb[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] frmDetails[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] objCommand [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbCommand[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] objDataAdapter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] objDataTable [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] mTable [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Buyer"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] intRowSelected [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmDetails_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2]OpenConnection()[/SIZE]
[SIZE=2]ShowData()[/SIZE]
[SIZE=2]CloseConnection()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'Binds the DataGridView & Text Boxes with the given table and shows data[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ShowData()[/SIZE]
[SIZE=2]txtID.DataBindings.Clear()[/SIZE]
[SIZE=2]txtDescription.DataBindings.Clear()[/SIZE]
[SIZE=2]grdDisplay.DataBindings.Clear()[/SIZE]
[SIZE=2]lblStatus.Text = [/SIZE][SIZE=2][COLOR=#a31515]"Total "[/COLOR][/SIZE]
[SIZE=2]objCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommand([/SIZE][SIZE=2][COLOR=#a31515]"Select * From "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table, objConnection)[/SIZE]
[SIZE=2]objDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter[/SIZE]
[SIZE=2]objDataTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]objDataAdapter.SelectCommand = objCommand[/SIZE]
[SIZE=2]objDataAdapter.Fill(objDataTable)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] grdDisplay[/SIZE]
[SIZE=2].DataSource = objDataTable[/SIZE]
[SIZE=2].AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke[/SIZE]
[SIZE=2].CellBorderStyle = DataGridViewCellBorderStyle.None[/SIZE]
[SIZE=2].SelectionMode = DataGridViewSelectionMode.FullRowSelect[/SIZE]
[SIZE=2].MultiSelect = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2].Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight[/SIZE]
[SIZE=2].Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight[/SIZE]
[SIZE=2].Columns(0).Width = 75[/SIZE]
[SIZE=2].Columns(1).Width = 300[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'Bind text boxes with the fields, like ColorId, BuyerDescription etc.[/COLOR][/SIZE]
[SIZE=2]txtID.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], objDataTable, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table & [/SIZE][SIZE=2][COLOR=#a31515]"ID"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]txtDescription.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], objDataTable, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table & [/SIZE][SIZE=2][COLOR=#a31515]"Description"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#008000]'Show the total record number in the status label[/COLOR][/SIZE]
[SIZE=2]lblStatus.Text &= [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table & [/SIZE][SIZE=2][COLOR=#a31515]" : "[/COLOR][/SIZE][SIZE=2] & objDataTable.Rows.Count.ToString[/SIZE]
 
[SIZE=2][COLOR=#008000]'Change the GroupBox Caption[/COLOR][/SIZE]
[SIZE=2]grpDetails.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table & [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] & grpDetails.Text[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] OleDbEx [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbException[/SIZE]
[SIZE=2]MessageBox.Show(OleDbEx.Message.ToString)[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataException[/SIZE]
[SIZE=2]MessageBox.Show(ex.Message.ToString)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'Set the form caption according to the table[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table[/SIZE]
 
[SIZE=2][COLOR=#008000]'Clear components from the memory[/COLOR][/SIZE]
[SIZE=2]objCommand.Dispose()[/SIZE]
[SIZE=2]objCommand = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]objDataAdapter.Dispose()[/SIZE]
[SIZE=2]objDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]objDataTable.Dispose()[/SIZE]
[SIZE=2]objDataTable = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'This property will be used to pass a table name to[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'the Details form while loading to show the data.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Table() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2]Table = mTable[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]mTable = value[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]

Now if someone please tell me how to provide functionality for my Add, Save, Cancel, Update, and delete button, it would be a great help. I am having difficulties with these functionalities..
 
i do all my stuff in code too.. every time i open DataSet1.Designer.vb i see lots of vb code doing all my database access things..
 
Back
Top