Following Code will Help you.
textbox databinding to a Sql Database.
Nov 06, 2006 07:24 PM | LINK
Not sure if anyone else is banging their head or not.
However I here is how to databind a textbox to a SQL Database using a sqldata control.
Here is the code behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["Title"].DefaultValue = txtTitle.Text.ToString();
SqlDataSource1.InsertParameters["Body"].DefaultValue = txtBody2.Text.ToString();
SqlDataSource1.InsertParameters["StartDate"].DefaultValue = MyDateStartTB1.Text.ToString();
SqlDataSource1.InsertParameters["EndDate"].DefaultValue = MyDateEndTB2.Text.ToString();
SqlDataSource1.Insert();
}
}
the Source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:local %>"
DeleteCommand="DELETE FROM [tblPageContent] WHERE [ID] = @ID" InsertCommand="INSERT INTO [tblPageContent] ([StartDate], [EndDate], [Title], [Body]) VALUES (@StartDate, @EndDate, @Title, @Body)"
SelectCommand="SELECT * FROM [tblPageContent]" UpdateCommand="UPDATE [tblPageContent] SET [StartDate] = @StartDate, [EndDate] = @EndDate, [Title] = @Title, [Body] = @Body WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="StartDate" Type="DateTime" />
<asp:Parameter Name="EndDate" Type="DateTime" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Body" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="StartDate" Type="DateTime" />
<asp:Parameter Name="EndDate" Type="DateTime" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Body" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Not too hard.
Reference:
http://www.aspnettutorials.com/tutorials/database/connect-sql-datasource-csharp.aspx
Enjoy!
its basic code you can mold it as per your requirments.
Thanks
Yogesh