By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Inserting a new row.

P: n/a
Hi,
I want to share this problem. I have a datagrid that will help me
Insert data into sql database. So I made a button On my form so that
when I press the button a new row on datagrid should be created and I
could be able to insert data. But with this code below I've failed
could someone help me and tell me where I'm going wrong:

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{

Fill();
Bind();
}
}
DataTable table = new DataTable();
private void Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);
}
private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();
}

private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Update";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}

private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
break;

case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
//InsertEmpty();
Bind();
}

Thanks.

Feb 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 28, 10:10 am, "rcoco" <nclau...@yahoo.cawrote:
Hi,
I want to share this problem. I have a datagrid that will help me
Insert data into sql database. So I made a button On my form so that
when I press the button a new row on datagrid should be created and I
could be able to insert data. But with this code below I've failed
could someone help me and tell me where I'm going wrong:

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{

Fill();
Bind();
}}

DataTable table = new DataTable();
private void Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);}

private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();

}

private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);

}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Update";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();

}

private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
break;

case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;

}

// fill and bind
Fill();
//InsertEmpty();
Bind();

}

Thanks.
Please specify what it fails. Do you get an error message or anything?
I've tested the code and it works for me, other than the fact that it
won't save the data because you have no code in the Update command.
But it does add a new row at the top of the datagrid with textboxes so
you can enter data and press the Update link.

-Brandon

Feb 28 '07 #2

P: n/a
Thanks,
I don't get any error message but what it can't do is When I press the
button to insert a new row it won't insert any new row. I added the
insert command but it won't change any thing. If you press the button
does it bing a new row? Because When I press button nothing Change
It's like it's not even related to the datagrid. after inserting the
insert code the code looks like this. Try for me and see if this is
ok:

DataTable table = new DataTable();
private DataTable Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);
return table;
}

private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();
}

private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
System.Web.UI.WebControls.TextBox st=new
System.Web.UI.WebControls.TextBox();
st=(System.Web.UI.WebControls.TextBox)e.Item.Cells[5].FindControl("txtobjach");
System.Web.UI.WebControls.TextBox st1=new
System.Web.UI.WebControls.TextBox();
st1=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[4].FindControl("txtobj");
System.Web.UI.WebControls.TextBox st2=new
System.Web.UI.WebControls.TextBox();
st2=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[3].Controls[0];
System.Web.UI.WebControls.TextBox st3=new
System.Web.UI.WebControls.TextBox();
st3=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[2].Controls[0];
System.Web.UI.WebControls.TextBox st4=new
System.Web.UI.WebControls.TextBox();
st4=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[1].Controls[0];
System.Web.UI.WebControls.TextBox st5=new
System.Web.UI.WebControls.TextBox();
st5=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[0].Controls[0];
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into DashBoard
(ObjectiveAchieved,Objective) values
('@ObjectiveAchieved' ,'@objective');";
myCommand.Parameters.Add(new
SqlParameter("@ObjectiveAchieved",SqlDbType.Char,4 5));
myCommand.Parameters["@ObjectiveAchieved"].Value=st.Text;
myCommand.Parameters.Add(new SqlParameter("@Objective",SqlDbType.Char,
45));
myCommand.Parameters["@Objective"].Value=st1.Text;
myCommand.Parameters.Add(new SqlParameter("@Workinghrs",SqlDbType.Char,
45));
myCommand.Parameters["@Workinghrs"].Value=st2.Text;
myCommand.Parameters.Add(new SqlParameter("@Loggedout",SqlDbType.Char,
45));
myCommand.Parameters["@Loggedout"].Value=st3.Text;
myCommand.Parameters.Add(new SqlParameter("@Loggedin",SqlDbType.Char,
45));
myCommand.Parameters["@Loggedin"].Value=st4.Text;
myCommand.Parameters.Add(new SqlParameter("@MoodToday",SqlDbType.Char,
45));
myCommand.Parameters["@MoodToday"].Value=st5.Text;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgis.EditItemIndex=-1;
BindDataGrid();
break;

case "Update":
break;

case "Cancel":
break;

case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Thanks

Mar 1 '07 #3

P: n/a
Hi there,

Handle RowEditing event and set grids view EditIndex property not
e.NewEditIndex. I prepared fully working example:

<asp:GridView runat="server" ID="dg" AutoGenerateColumns="false"
OnRowEditing="dg_RowEditing"
ShowFooter="true" OnRowUpdating="dg_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%# Bind("Name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button runat="server" CommandName="Edit" ID="btnAddNew" Text="New" />
<asp:Button runat="server" CommandName="Update" ID="btnUpdate"
Text="Update" Visible="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dg.DataSource = GetData();
dg.DataBind();
}
}

private System.Data.DataTable GetData()
{
System.Data.DataTable dataTable =
new System.Data.DataTable();

dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));

for (int i = 0; i < this.RowCount; i++)
{
System.Data.DataRow r = dataTable.NewRow();
r[0] = i;
r[1] = "name" + i.ToString();
dataTable.Rows.Add(r);
}

return dataTable;
}

protected void dg_RowEditing(object sender, GridViewEditEventArgs e)
{
System.Data.DataTable dataTable = GetData();
dataTable.Rows.InsertAt(dataTable.NewRow(), 0);

GridView gridView = (GridView)sender;
gridView.EditIndex = 0;

dg.DataSource = dataTable;
dg.DataBind();

Button button;
button = (Button)gridView.FooterRow.FindControl("btnUpdate" );
button.Visible = true;
button = (Button)gridView.FooterRow.FindControl("btnAddNew" );
button.Visible = false;
}
protected void dg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gridView = (GridView)sender;
TextBox textBox = (TextBox)gridView.Rows[0].FindControl("txtName");

string newName = textBox.Text;

// do something with name and insert to database

// simulate a new row has been added
RowCount++;

// go back to readonlt mode
gridView.EditIndex = -1;
gridView.DataSource = GetData();
gridView.DataBind();
}

/// <summary>
/// I only use it to simulate new record was added
/// </summary>
private int RowCount
{
get
{
object value = ViewState["RowCount"];
return value == null ? 5 : (int)value;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
ViewState["RowCount"] = value;
}
}
</script>
Hope it helps
--
Milosz
--
Milosz
"rcoco" wrote:
Thanks,
I don't get any error message but what it can't do is When I press the
button to insert a new row it won't insert any new row. I added the
insert command but it won't change any thing. If you press the button
does it bing a new row? Because When I press button nothing Change
It's like it's not even related to the datagrid. after inserting the
insert code the code looks like this. Try for me and see if this is
ok:

DataTable table = new DataTable();
private DataTable Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);
return table;
}

private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();
}

private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
System.Web.UI.WebControls.TextBox st=new
System.Web.UI.WebControls.TextBox();
st=(System.Web.UI.WebControls.TextBox)e.Item.Cells[5].FindControl("txtobjach");
System.Web.UI.WebControls.TextBox st1=new
System.Web.UI.WebControls.TextBox();
st1=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[4].FindControl("txtobj");
System.Web.UI.WebControls.TextBox st2=new
System.Web.UI.WebControls.TextBox();
st2=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[3].Controls[0];
System.Web.UI.WebControls.TextBox st3=new
System.Web.UI.WebControls.TextBox();
st3=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[2].Controls[0];
System.Web.UI.WebControls.TextBox st4=new
System.Web.UI.WebControls.TextBox();
st4=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[1].Controls[0];
System.Web.UI.WebControls.TextBox st5=new
System.Web.UI.WebControls.TextBox();
st5=(System.Web.UI.WebControls.TextBox)e.Item.Cell s[0].Controls[0];
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into DashBoard
(ObjectiveAchieved,Objective) values
('@ObjectiveAchieved' ,'@objective');";
myCommand.Parameters.Add(new
SqlParameter("@ObjectiveAchieved",SqlDbType.Char,4 5));
myCommand.Parameters["@ObjectiveAchieved"].Value=st.Text;
myCommand.Parameters.Add(new SqlParameter("@Objective",SqlDbType.Char,
45));
myCommand.Parameters["@Objective"].Value=st1.Text;
myCommand.Parameters.Add(new SqlParameter("@Workinghrs",SqlDbType.Char,
45));
myCommand.Parameters["@Workinghrs"].Value=st2.Text;
myCommand.Parameters.Add(new SqlParameter("@Loggedout",SqlDbType.Char,
45));
myCommand.Parameters["@Loggedout"].Value=st3.Text;
myCommand.Parameters.Add(new SqlParameter("@Loggedin",SqlDbType.Char,
45));
myCommand.Parameters["@Loggedin"].Value=st4.Text;
myCommand.Parameters.Add(new SqlParameter("@MoodToday",SqlDbType.Char,
45));
myCommand.Parameters["@MoodToday"].Value=st5.Text;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgis.EditItemIndex=-1;
BindDataGrid();
break;

case "Update":
break;

case "Cancel":
break;

case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Thanks

Mar 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.