EDN Admin
Well-known member
hi guys,
Add row in datagridview that is bounded to datatable and save to database
i am newbie in c#.net and I am sure you know exactly what to do and help on my concern. Well, I was on my way to finish my project of an simple ordering system. There will be time that the user would like to add a new row to the grid that was
already in the database. I would like to ask your if you have a simple source code that will guide me on how to do this.
Well below is the code that I have.
I have 2 forms and 1 BAL Class
Here is my BALClass
public class BLLClass<br/>
{<br/>
#region VARIABLE<br/>
clsDataAccess dbAccess = new clsDataAccess();<br/>
string _selectedID;<br/>
<br/>
#endregion<br/>
<br/>
#region OBJECT<br/>
public DataTable dtClass { get; set; }<br/>
public DataRow drClass { get; set; }<br/>
#endregion<br/>
<br/>
public BLLClass()<br/>
{<br/>
FillGrid();<br/>
}<br/>
<br/>
public DataTable FillGrid()<br/>
{<br/>
try<br/>
{<br/>
dtClass = dbAccess.ExecuteQuery("select * from productClass", CommandType.Text);<br/>
return dtClass;<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public string CurrentID<br/>
{<br/>
get { return _selectedID; }<br/>
<br/>
set<br/>
{<br/>
_selectedID = value;<br/>
DataTable dt;<br/>
try<br/>
{<br/>
<br/>
dbAccess.ClearParameters();<br/>
dbAccess.CreateParamater("@ProductClassID", _selectedID);<br/>
dt = dbAccess.ExecuteQuery("select * from productclass where productClassID = @ProductClassID");<br/>
<br/>
if (dt.Rows.Count > 0)<br/>
drClass = dt.Rows[0];<br/>
<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
}<br/>
<br/>
public void NewRecord()<br/>
{<br/>
drClass = dtClass.NewRow();<br/>
}<br/>
<br/>
public void AddItem()<br/>
{<br/>
try<br/>
{<br/>
dtClass.Rows.Add(drClass);<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public void Save()<br/>
{<br/>
try<br/>
{<br/>
dbAccess.ClearParameters();<br/>
dbAccess.CreateParamater("@ProductClassID", drClass["ProductClassID"]);<br/>
dbAccess.CreateParamater("@Description", drClass["Description"]);<br/>
//dbAccess.CreateParamater("@UpdateDate", drClass["UpdateDate"]);<br/>
dbAccess.CreateParamater("@ActiveUser", drClass["UpdateUser"]);<br/>
<br/>
dbAccess.ExecuteQuery("UpdateProductClass", CommandType.StoredProcedure);<br/>
<br/>
updateHeader();<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public void updateHeader()<br/>
{<br/>
try<br/>
{<br/>
DataRow[] fRow = dtClass.Select("ProductClassID = " + _selectedID + "");<br/>
if (fRow.Length > 0)<br/>
for (int i = 0; i < dtClass.Columns.Count; i++)<br/>
fRow[0] = drClass;<br/>
}<br/>
catch (Exception ex) { new Exception("Error in updateHeader() :/n" + ex.Message); }<br/>
}<br/>
} <br/>
<br/>
}<br/>
<br/>
Form 1
public partial class Form1 : Form<br/>
{<br/>
#region VARIABLE<br/>
BLLClass balClass = new BLLClass();<br/>
#endregion<br/>
<br/>
public Form1()<br/>
{<br/>
InitializeComponent();<br/>
}<br/>
<br/>
private void Form1_Load(object sender, EventArgs e)<br/>
{<br/>
dgv.DataSource = balClass.FillGrid();<br/>
}<br/>
<br/>
private void dgv_DoubleClick(object sender, EventArgs e)<br/>
{<br/>
try<br/>
{<br/>
string value = dgv.CurrentRow.Cells[0].Value.ToString();<br/>
balClass.CurrentID = value;<br/>
Form2 frm2 = new Form2(balClass);<br/>
frm2.ShowDialog();<br/>
<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
<br/>
private void btnRemove_Click(object sender, EventArgs e)<br/>
{<br/>
DialogResult res = MessageBox.Show("Buburahin mo ba talaga ito? Sure ka?", "Reminder", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);<br/>
<br/>
if (res == DialogResult.Yes)<br/>
dgv.Rows.RemoveAt(dgv.CurrentRow.Index);
<br/>
}<br/>
<br/>
private void btnProcess_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Please enter Description", "Required Input", MessageBoxButtons.OK, MessageBoxIcon.Information);<br/>
txtDescription.Focus();<br/>
return;<br/>
}
<br/>
<br/>
int b = 0;<br/>
while (b <= dgv.Rows.Count - 1)<br/>
{<br/>
for (int a = 0; a <= (dgv.Columns.Count - 1); a++)<br/>
{<br/>
balClass.drClass["ProductClassID"] = code.ToInt32(dgv.CurrentRow.Cells["ProductClassID"].Value.ToString());<br/>
balClass.drClass["Description"] = dgv.CurrentRow.Cells["Description"].Value.ToString();<br/>
//balClass.drClass["UpdateDate"] = dgv.CurrentRow.Cells["UpdateDate"].Value.ToString();<br/>
balClass.drClass["UpdateUser"] = dgv.CurrentRow.Cells["UpdateUser"].Value.ToString();<br/>
<br/>
balClass.Save();<br/>
b = b + 1;<br/>
<br/>
if (b == dgv.Rows.Count)<br/>
return;<br/>
}<br/>
}<br/>
//balClass.FillGrid();<br/>
}<br/>
<br/>
private void btnAdd_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Please enter Description", "Required Input", MessageBoxButtons.OK, MessageBoxIcon.Information);<br/>
txtDescription.Focus();<br/>
return;<br/>
}<br/>
<br/>
balClass.NewRecord();<br/>
try<br/>
{<br/>
balClass.drClass["Description"] = txtDescription.Text.Trim();<br/>
balClass.drClass["UpdateDate"] = DateTime.Now;<br/>
balClass.drClass["UpdateUser"] = "anthrow";<br/>
<br/>
balClass.AddItem();<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
}<br/>
}
= ==== ==
this is my FORM2
public partial class Form2 : Form<br/>
{<br/>
#region VARIBLE<br/>
BLLClass balClass;<br/>
<br/>
#endregion<br/>
<br/>
public Form2(object clsFrm1)<br/>
{<br/>
InitializeComponent();<br/>
balClass = (BLLClass)clsFrm1;<br/>
}<br/>
<br/>
private void Form2_Load(object sender, EventArgs e)<br/>
{<br/>
SetForm();<br/>
}<br/>
<br/>
private void SetForm()<br/>
{
<br/>
txtID.Text = balClass.drClass["ProductClassID"].ToString();<br/>
txtDescription.Text = balClass.drClass["Description"].ToString();<br/>
txtUpdateDate.Text = balClass.drClass["UpdateDate"].ToString();<br/>
txtUpdateUser.Text = balClass.drClass["UpdateUser"].ToString();
<br/>
}<br/>
<br/>
private void btnSave_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Description is required!");<br/>
return;<br/>
}<br/>
<br/>
try<br/>
{<br/>
balClass.drClass["ProductClassID"] = txtID.Text;<br/>
balClass.drClass["Description"] = txtDescription.Text.Trim();<br/>
balClass.drClass["UpdateDate"] = DateTime.Now;<br/>
balClass.drClass["UpdateUser"] = "anthrow";<br/>
<br/>
balClass.Save();<br/>
this.Close();<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
}<br/>
}
= = = =
SCRIPT OF MY TABLE
USE [DUMMY]<br/>
GO<br/>
<br/>
/****** Object: Table [dbo].[ProductClass] Script Date: 09/16/2011 14:30:12 ******/<br/>
SET ANSI_NULLS ON<br/>
GO<br/>
<br/>
SET QUOTED_IDENTIFIER ON<br/>
GO<br/>
<br/>
SET ANSI_PADDING ON<br/>
GO<br/>
<br/>
CREATE TABLE [dbo].[ProductClass](<br/>
[ProductClassID] [int] IDENTITY(1,1) NOT NULL,<br/>
[Description] [varchar](50) NULL,<br/>
[UpdateDate] [datetime] NULL,<br/>
[UpdateUser] [varchar](50) NULL,<br/>
[ObsoleteDate] [date] NULL<br/>
) ON [PRIMARY]<br/>
<br/>
GO<br/>
<br/>
SET ANSI_PADDING OFF<br/>
GO<br/>
<br/>
<br/>
= = ==
here is my update SP
ALTER PROCEDURE [dbo].[UpdateProductClass]<br/>
-- Add the parameters for the stored procedure here<br/>
( <br/>
@ProductClassID int OUTPUT,<br/>
@Description varchar(20),<br/>
@ObsoleteDate datetime = NULL,<br/>
@ActiveUser varchar(30) <br/>
)<br/>
<br/>
AS <br/>
SET NOCOUNT ON;<br/>
<br/>
IF EXISTS(SELECT 1 FROM ProductClass WHERE ProductClassID = @ProductClassID)<br/>
BEGIN<br/>
UPDATE ProductClass SET<br/>
<br/>
Description = @Description,<br/>
ObsoleteDate = @ObsoleteDate,
<br/>
UpdateDate = GETDATE(),<br/>
UpdateUser = @ActiveUser<br/>
<br/>
WHERE ProductClassID = @ProductClassID<br/>
END<br/>
<br/>
ELSE<br/>
<br/>
BEGIN<br/>
INSERT INTO ProductClass<br/>
(
<br/>
Description,<br/>
ObsoleteDate,<br/>
UpdateDate,<br/>
UpdateUser<br/>
)<br/>
VALUES<br/>
(
<br/>
@Description,<br/>
NULL, <br/>
GETDATE(),<br/>
@ActiveUser
<br/>
)<br/>
<br/>
SET @ProductClassID = SCOPE_IDENTITY()<br/>
<br/>
END<br/>
SET NOCOUNT OFF<br/>
SELECT @ProductClassID as ProductClassID
= = ==
There is problem when saving new row in database.
If you could provide me a sample working code same like this.
<
Christian<br/>
View the full article
Add row in datagridview that is bounded to datatable and save to database
i am newbie in c#.net and I am sure you know exactly what to do and help on my concern. Well, I was on my way to finish my project of an simple ordering system. There will be time that the user would like to add a new row to the grid that was
already in the database. I would like to ask your if you have a simple source code that will guide me on how to do this.
Well below is the code that I have.
I have 2 forms and 1 BAL Class
Here is my BALClass
public class BLLClass<br/>
{<br/>
#region VARIABLE<br/>
clsDataAccess dbAccess = new clsDataAccess();<br/>
string _selectedID;<br/>
<br/>
#endregion<br/>
<br/>
#region OBJECT<br/>
public DataTable dtClass { get; set; }<br/>
public DataRow drClass { get; set; }<br/>
#endregion<br/>
<br/>
public BLLClass()<br/>
{<br/>
FillGrid();<br/>
}<br/>
<br/>
public DataTable FillGrid()<br/>
{<br/>
try<br/>
{<br/>
dtClass = dbAccess.ExecuteQuery("select * from productClass", CommandType.Text);<br/>
return dtClass;<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public string CurrentID<br/>
{<br/>
get { return _selectedID; }<br/>
<br/>
set<br/>
{<br/>
_selectedID = value;<br/>
DataTable dt;<br/>
try<br/>
{<br/>
<br/>
dbAccess.ClearParameters();<br/>
dbAccess.CreateParamater("@ProductClassID", _selectedID);<br/>
dt = dbAccess.ExecuteQuery("select * from productclass where productClassID = @ProductClassID");<br/>
<br/>
if (dt.Rows.Count > 0)<br/>
drClass = dt.Rows[0];<br/>
<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
}<br/>
<br/>
public void NewRecord()<br/>
{<br/>
drClass = dtClass.NewRow();<br/>
}<br/>
<br/>
public void AddItem()<br/>
{<br/>
try<br/>
{<br/>
dtClass.Rows.Add(drClass);<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public void Save()<br/>
{<br/>
try<br/>
{<br/>
dbAccess.ClearParameters();<br/>
dbAccess.CreateParamater("@ProductClassID", drClass["ProductClassID"]);<br/>
dbAccess.CreateParamater("@Description", drClass["Description"]);<br/>
//dbAccess.CreateParamater("@UpdateDate", drClass["UpdateDate"]);<br/>
dbAccess.CreateParamater("@ActiveUser", drClass["UpdateUser"]);<br/>
<br/>
dbAccess.ExecuteQuery("UpdateProductClass", CommandType.StoredProcedure);<br/>
<br/>
updateHeader();<br/>
}<br/>
catch { throw; }<br/>
}<br/>
<br/>
public void updateHeader()<br/>
{<br/>
try<br/>
{<br/>
DataRow[] fRow = dtClass.Select("ProductClassID = " + _selectedID + "");<br/>
if (fRow.Length > 0)<br/>
for (int i = 0; i < dtClass.Columns.Count; i++)<br/>
fRow[0] = drClass;<br/>
}<br/>
catch (Exception ex) { new Exception("Error in updateHeader() :/n" + ex.Message); }<br/>
}<br/>
} <br/>
<br/>
}<br/>
<br/>
Form 1
public partial class Form1 : Form<br/>
{<br/>
#region VARIABLE<br/>
BLLClass balClass = new BLLClass();<br/>
#endregion<br/>
<br/>
public Form1()<br/>
{<br/>
InitializeComponent();<br/>
}<br/>
<br/>
private void Form1_Load(object sender, EventArgs e)<br/>
{<br/>
dgv.DataSource = balClass.FillGrid();<br/>
}<br/>
<br/>
private void dgv_DoubleClick(object sender, EventArgs e)<br/>
{<br/>
try<br/>
{<br/>
string value = dgv.CurrentRow.Cells[0].Value.ToString();<br/>
balClass.CurrentID = value;<br/>
Form2 frm2 = new Form2(balClass);<br/>
frm2.ShowDialog();<br/>
<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
<br/>
private void btnRemove_Click(object sender, EventArgs e)<br/>
{<br/>
DialogResult res = MessageBox.Show("Buburahin mo ba talaga ito? Sure ka?", "Reminder", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);<br/>
<br/>
if (res == DialogResult.Yes)<br/>
dgv.Rows.RemoveAt(dgv.CurrentRow.Index);
<br/>
}<br/>
<br/>
private void btnProcess_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Please enter Description", "Required Input", MessageBoxButtons.OK, MessageBoxIcon.Information);<br/>
txtDescription.Focus();<br/>
return;<br/>
}
<br/>
<br/>
int b = 0;<br/>
while (b <= dgv.Rows.Count - 1)<br/>
{<br/>
for (int a = 0; a <= (dgv.Columns.Count - 1); a++)<br/>
{<br/>
balClass.drClass["ProductClassID"] = code.ToInt32(dgv.CurrentRow.Cells["ProductClassID"].Value.ToString());<br/>
balClass.drClass["Description"] = dgv.CurrentRow.Cells["Description"].Value.ToString();<br/>
//balClass.drClass["UpdateDate"] = dgv.CurrentRow.Cells["UpdateDate"].Value.ToString();<br/>
balClass.drClass["UpdateUser"] = dgv.CurrentRow.Cells["UpdateUser"].Value.ToString();<br/>
<br/>
balClass.Save();<br/>
b = b + 1;<br/>
<br/>
if (b == dgv.Rows.Count)<br/>
return;<br/>
}<br/>
}<br/>
//balClass.FillGrid();<br/>
}<br/>
<br/>
private void btnAdd_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Please enter Description", "Required Input", MessageBoxButtons.OK, MessageBoxIcon.Information);<br/>
txtDescription.Focus();<br/>
return;<br/>
}<br/>
<br/>
balClass.NewRecord();<br/>
try<br/>
{<br/>
balClass.drClass["Description"] = txtDescription.Text.Trim();<br/>
balClass.drClass["UpdateDate"] = DateTime.Now;<br/>
balClass.drClass["UpdateUser"] = "anthrow";<br/>
<br/>
balClass.AddItem();<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
}<br/>
}
= ==== ==
this is my FORM2
public partial class Form2 : Form<br/>
{<br/>
#region VARIBLE<br/>
BLLClass balClass;<br/>
<br/>
#endregion<br/>
<br/>
public Form2(object clsFrm1)<br/>
{<br/>
InitializeComponent();<br/>
balClass = (BLLClass)clsFrm1;<br/>
}<br/>
<br/>
private void Form2_Load(object sender, EventArgs e)<br/>
{<br/>
SetForm();<br/>
}<br/>
<br/>
private void SetForm()<br/>
{
<br/>
txtID.Text = balClass.drClass["ProductClassID"].ToString();<br/>
txtDescription.Text = balClass.drClass["Description"].ToString();<br/>
txtUpdateDate.Text = balClass.drClass["UpdateDate"].ToString();<br/>
txtUpdateUser.Text = balClass.drClass["UpdateUser"].ToString();
<br/>
}<br/>
<br/>
private void btnSave_Click(object sender, EventArgs e)<br/>
{<br/>
if (txtDescription.Text.Trim() == string.Empty)<br/>
{<br/>
MessageBox.Show("Description is required!");<br/>
return;<br/>
}<br/>
<br/>
try<br/>
{<br/>
balClass.drClass["ProductClassID"] = txtID.Text;<br/>
balClass.drClass["Description"] = txtDescription.Text.Trim();<br/>
balClass.drClass["UpdateDate"] = DateTime.Now;<br/>
balClass.drClass["UpdateUser"] = "anthrow";<br/>
<br/>
balClass.Save();<br/>
this.Close();<br/>
}<br/>
catch (Exception ex) { MessageBox.Show(ex.Message); }<br/>
}<br/>
}<br/>
}
= = = =
SCRIPT OF MY TABLE
USE [DUMMY]<br/>
GO<br/>
<br/>
/****** Object: Table [dbo].[ProductClass] Script Date: 09/16/2011 14:30:12 ******/<br/>
SET ANSI_NULLS ON<br/>
GO<br/>
<br/>
SET QUOTED_IDENTIFIER ON<br/>
GO<br/>
<br/>
SET ANSI_PADDING ON<br/>
GO<br/>
<br/>
CREATE TABLE [dbo].[ProductClass](<br/>
[ProductClassID] [int] IDENTITY(1,1) NOT NULL,<br/>
[Description] [varchar](50) NULL,<br/>
[UpdateDate] [datetime] NULL,<br/>
[UpdateUser] [varchar](50) NULL,<br/>
[ObsoleteDate] [date] NULL<br/>
) ON [PRIMARY]<br/>
<br/>
GO<br/>
<br/>
SET ANSI_PADDING OFF<br/>
GO<br/>
<br/>
<br/>
= = ==
here is my update SP
ALTER PROCEDURE [dbo].[UpdateProductClass]<br/>
-- Add the parameters for the stored procedure here<br/>
( <br/>
@ProductClassID int OUTPUT,<br/>
@Description varchar(20),<br/>
@ObsoleteDate datetime = NULL,<br/>
@ActiveUser varchar(30) <br/>
)<br/>
<br/>
AS <br/>
SET NOCOUNT ON;<br/>
<br/>
IF EXISTS(SELECT 1 FROM ProductClass WHERE ProductClassID = @ProductClassID)<br/>
BEGIN<br/>
UPDATE ProductClass SET<br/>
<br/>
Description = @Description,<br/>
ObsoleteDate = @ObsoleteDate,
<br/>
UpdateDate = GETDATE(),<br/>
UpdateUser = @ActiveUser<br/>
<br/>
WHERE ProductClassID = @ProductClassID<br/>
END<br/>
<br/>
ELSE<br/>
<br/>
BEGIN<br/>
INSERT INTO ProductClass<br/>
(
<br/>
Description,<br/>
ObsoleteDate,<br/>
UpdateDate,<br/>
UpdateUser<br/>
)<br/>
VALUES<br/>
(
<br/>
@Description,<br/>
NULL, <br/>
GETDATE(),<br/>
@ActiveUser
<br/>
)<br/>
<br/>
SET @ProductClassID = SCOPE_IDENTITY()<br/>
<br/>
END<br/>
SET NOCOUNT OFF<br/>
SELECT @ProductClassID as ProductClassID
= = ==
There is problem when saving new row in database.
If you could provide me a sample working code same like this.
<
Christian<br/>
View the full article