Codeaddict7423
asked on
asp.net c# insert data into databse table
Hello,
I have an application where I'm attempting to add data to a database table and I'm having difficulties with my code. Spefically, I have a page with a gridview control populated from a database table.
My *.aspx code follows:
-----------------
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View_Bureau" runat="server">
<table class="style1" width="500" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table class="modalpnl">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grdBureau" runat="server" BackColor="LightGoldenrodY ellow" BorderColor="Tan" AllowSorting="True" ForeColor="Black" AutoGenerateColumns="False " CellPadding="2" DataKeyNames="BureauID" DataSourceID="SqlDataSourc e1" EmptyDataText="There are no data records to display." Font-Names="Arial" Font-Size="Small" GridLines="None" BorderWidth="1px">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Bureau" HeaderText="Bureau" SortExpression="Bureau" />
<asp:BoundField DataField="Activate" HeaderText="Activate" SortExpression="Activate" />
<asp:BoundField DataField="EnterBy" HeaderText="EnterBy" SortExpression="EnterBy" />
<asp:BoundField DataField="EnterDate" HeaderText="EnterDate" SortExpression="EnterDate" />
</Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BadgeCod eTablesCon nectionStr ing1 %>"
DeleteCommand="DELETE FROM [Bureau] WHERE [BureauID] = @BureauID" InsertCommand="INSERT INTO [Bureau] ([BureauID], [Bureau], [Activate], [EnterBy], [EnterDate]) VALUES (@BureauID, @Bureau, @Activate, @EnterBy, @EnterDate)"
ProviderName="<%$ ConnectionStrings:BadgeCod eTablesCon nectionStr ing1.Provi derName %>"
SelectCommand="SELECT [BureauID], [Bureau], [Activate], [EnterBy], [EnterDate] FROM [Bureau]"
UpdateCommand="UPDATE [Bureau] SET [Bureau] = @Bureau, [Activate] = @Activate, [EnterBy] = @EnterBy, [EnterDate] = @EnterDate WHERE [BureauID] = @BureauID">
<DeleteParameters>
<asp:Parameter Name="BureauID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="BureauID" Type="Int32" />
<asp:Parameter Name="Bureau" Type="String" />
<asp:Parameter Name="Activate" Type="String" />
<asp:Parameter Name="EnterBy" Type="String" />
<asp:Parameter Name="EnterDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Bureau" Type="String" />
<asp:Parameter Name="Activate" Type="String" />
<asp:Parameter Name="EnterBy" Type="String" />
<asp:Parameter Name="EnterDate" Type="DateTime" />
<asp:Parameter Name="BureauID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
--------------
My codebehind follows:
--------------
protected void btnsavebureau_Click(object sender, EventArgs e)
{
con = new SqlConnection(Configuratio nManager.C onnectionS trings["Ba dgeCodeTab lesConnect ionString" ].Connecti onString);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = @"INSERT INTO [[BadgeCodeTables] ([Bureau], [Activate],[enterby], [enterdate]) VALUES (@bureauname,'True',@usern ame, getdate))";
cmd.Parameters.AddWithValu e("@bureau name", txtAddBureauname.Text);
cmd.Parameters.AddWithValu e("@Activa te", 1);
cmd.Parameters.AddWithValu e("@userna me", 1);
cmd.Parameters.AddWithValu e("@getdat e", 1);
cmd.ExecuteNonQuery();
con.Close();
txtAddBureauname.Text = " ";
con.Dispose();
Response.Redirect("Adminis tration.as px");
}
private string Username()
{
string username = HttpContext.Current.User.I dentity.Na me.ToStrin g();
return username;
}
-------------
I would like help with my codebehind so that I can add new items to this table (dbo.bureau) and refresh the page so the user can see the new data upon page reload.
ANY help would be greatly appreciated.
I have an application where I'm attempting to add data to a database table and I'm having difficulties with my code. Spefically, I have a page with a gridview control populated from a database table.
My *.aspx code follows:
-----------------
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View_Bureau" runat="server">
<table class="style1" width="500" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table class="modalpnl">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grdBureau" runat="server" BackColor="LightGoldenrodY
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Bureau" HeaderText="Bureau" SortExpression="Bureau" />
<asp:BoundField DataField="Activate" HeaderText="Activate" SortExpression="Activate" />
<asp:BoundField DataField="EnterBy" HeaderText="EnterBy" SortExpression="EnterBy" />
<asp:BoundField DataField="EnterDate" HeaderText="EnterDate" SortExpression="EnterDate"
</Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BadgeCod
DeleteCommand="DELETE FROM [Bureau] WHERE [BureauID] = @BureauID" InsertCommand="INSERT INTO [Bureau] ([BureauID], [Bureau], [Activate], [EnterBy], [EnterDate]) VALUES (@BureauID, @Bureau, @Activate, @EnterBy, @EnterDate)"
ProviderName="<%$ ConnectionStrings:BadgeCod
SelectCommand="SELECT [BureauID], [Bureau], [Activate], [EnterBy], [EnterDate] FROM [Bureau]"
UpdateCommand="UPDATE [Bureau] SET [Bureau] = @Bureau, [Activate] = @Activate, [EnterBy] = @EnterBy, [EnterDate] = @EnterDate WHERE [BureauID] = @BureauID">
<DeleteParameters>
<asp:Parameter Name="BureauID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="BureauID" Type="Int32" />
<asp:Parameter Name="Bureau" Type="String" />
<asp:Parameter Name="Activate" Type="String" />
<asp:Parameter Name="EnterBy" Type="String" />
<asp:Parameter Name="EnterDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Bureau" Type="String" />
<asp:Parameter Name="Activate" Type="String" />
<asp:Parameter Name="EnterBy" Type="String" />
<asp:Parameter Name="EnterDate" Type="DateTime" />
<asp:Parameter Name="BureauID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
--------------
My codebehind follows:
--------------
protected void btnsavebureau_Click(object
{
con = new SqlConnection(Configuratio
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = @"INSERT INTO [[BadgeCodeTables] ([Bureau], [Activate],[enterby], [enterdate]) VALUES (@bureauname,'True',@usern
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.ExecuteNonQuery();
con.Close();
txtAddBureauname.Text = " ";
con.Dispose();
Response.Redirect("Adminis
}
private string Username()
{
string username = HttpContext.Current.User.I
return username;
}
-------------
I would like help with my codebehind so that I can add new items to this table (dbo.bureau) and refresh the page so the user can see the new data upon page reload.
ANY help would be greatly appreciated.
ASKER
TheLearnedOne:
Thank you for your reply. Indeed, I'm having a problem with the btnsavebureau_Click in that it does not seem to save.
Any code block that you could provide to add to this database table would be appreciated.
The screen only needs the name of the bureau (bureauname), I'd insert the values: 'True',@username, getdate.
Thank you for your reply. Indeed, I'm having a problem with the btnsavebureau_Click in that it does not seem to save.
Any code block that you could provide to add to this database table would be appreciated.
The screen only needs the name of the bureau (bureauname), I'd insert the values: 'True',@username, getdate.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<asp:CommandField ShowEditButton="True" />
Are you having a problem with btnsavebureau_Click? What does that do differently from the CommandField?