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)
{
Sorry. What im saying is, if you have a datasource whom's insert command takes 3 parameters. You go in its properties, click on the little lightning bolt in the property window to access the events, find "inserting", and double click in the field. It will
generate for you the Inserting stub function for that event.
Then on your button, just call Insert() and thats it, like you were doing, but without the parameters.
The Inserting event's second parameter is "e" (you'll see it when you have the stub in front of you from doing the above ).
e is an object that contains all the paramters of the Inserting event. One of which is Command, which is a standard ADO.NET SqlCommand.
One of the properties of SqlCommand is parameters, which is a simple collection of your parameters. So using the syntax that confused you, you will be able to set those parameters -right- before the Insert is done (which is what the Inserting event is).
Thus by setting its values, you will be able to replicate what you were doing in your button click, but it will be done at the "right time", so to speak.
the code is underline with a blue line and when I place cursor over it, I'm told "Property access must assign to the property or use its value". I thought I was assigning the value with this statment. I'm using ASP.NET 2.0 frame work and VB.NET. When
I type the code manually and I get to this point:
e.Command.Parameters["fname"].
intellisense doesn't show DefaultValue as one of the options. I'm new at this, so I'm sure I must be missing something.
Here is the code:
Partial
Class GBTextBoxes
Inherits System.Web.UI.Page
Protected Sub SqlDataSource1_Inserting(ByVal sender
As Object,
ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
Handles SqlDataSource1.Inserting
e.Command.Parameters[
"fname"].DefaultValue = txtFirstName.Text.ToString()
End Sub
Thanks. It still doesn't work. I receive a different error:
DefaultValue is not a member of System.Data.Common.DBParameter.
So, I thought I would try typing the line of code again and see what options that intellsense would showed me. This time around I saw the Value property, so I will give this a go and see how it works out. Here is my new line of code:
mikedopp
Member
146 Points
19 Posts
textbox databinding to a Sql Database.
Nov 06, 2006 06: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!
Mike Dopp
shados
Star
12285 Points
2229 Posts
Re: textbox databinding to a Sql Database.
Nov 07, 2006 01:00 AM|LINK
There is a cleaner way of doing it.
By registering the "Inserting" (or Selecting, or Updating, or Deleting) event of the datasource. Then you can access all parameters like this:
e.Command.Parameters["Title"].DefaultValue = txtTitle.Text.ToString();
This way you only need to call the Insert method in your button click, and the logic is cleanly separated and is where it should be :)
mikedopp
Member
146 Points
19 Posts
Re: textbox databinding to a Sql Database.
Nov 14, 2006 06:11 PM|LINK
could you give me some pseudocode?
I am confused about the "e.command" syntax.
Also do you know how I can update the record after I have used the insert command?
Thanks,
Mike
shados
Star
12285 Points
2229 Posts
Re: textbox databinding to a Sql Database.
Nov 14, 2006 09:20 PM|LINK
Sorry. What im saying is, if you have a datasource whom's insert command takes 3 parameters. You go in its properties, click on the little lightning bolt in the property window to access the events, find "inserting", and double click in the field. It will generate for you the Inserting stub function for that event.
Then on your button, just call Insert() and thats it, like you were doing, but without the parameters.
The Inserting event's second parameter is "e" (you'll see it when you have the stub in front of you from doing the above ).
e is an object that contains all the paramters of the Inserting event. One of which is Command, which is a standard ADO.NET SqlCommand.
One of the properties of SqlCommand is parameters, which is a simple collection of your parameters. So using the syntax that confused you, you will be able to set those parameters -right- before the Insert is done (which is what the Inserting event is). Thus by setting its values, you will be able to replicate what you were doing in your button click, but it will be done at the "right time", so to speak.
Is that easier to understand?
KayBell
Member
10 Points
5 Posts
Re: textbox databinding to a Sql Database.
Nov 17, 2006 02:05 PM|LINK
This doesn't seem to work for me. When I try using:
e.Command.Parameters["fname"].DefaultValue = txtFirstName.Text.ToString()
the code is underline with a blue line and when I place cursor over it, I'm told "Property access must assign to the property or use its value". I thought I was assigning the value with this statment. I'm using ASP.NET 2.0 frame work and VB.NET. When I type the code manually and I get to this point:
e.Command.Parameters["fname"].
intellisense doesn't show DefaultValue as one of the options. I'm new at this, so I'm sure I must be missing something.
Here is the code:
Partial
Class GBTextBoxes Inherits System.Web.UI.Page Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Insertinge.Command.Parameters[
"fname"].DefaultValue = txtFirstName.Text.ToString() End SubEnd
ClassThanks for any help.
shados
Star
12285 Points
2229 Posts
Re: textbox databinding to a Sql Database.
Nov 17, 2006 07:27 PM|LINK
KayBell
Member
10 Points
5 Posts
Re: textbox databinding to a Sql Database.
Nov 20, 2006 08:41 PM|LINK
Thanks. It still doesn't work. I receive a different error:
DefaultValue is not a member of System.Data.Common.DBParameter.
So, I thought I would try typing the line of code again and see what options that intellsense would showed me. This time around I saw the Value property, so I will give this a go and see how it works out. Here is my new line of code:
e.Command.Parameters("fname").Value = txtFirstName.Text.ToString()
Thanks for your help.
Kay
mikedopp
Member
146 Points
19 Posts
Re: textbox databinding to a Sql Database.
Jan 03, 2007 07:37 PM|LINK
Yes... Thank you for answering that question for me and yes it is definitely easier.
Thank you for your help shados.
Mike
http://mikedopp.com
alug
Member
57 Points
20 Posts
Re: textbox databinding to a Sql Database.
Mar 28, 2007 01:20 PM|LINK
hi:
your code is nice but there some extram code
SqlDataSource1.InsertParameters["Title"].DefaultValue = txtTitle.Text.ToString();
You should not write the ToString() method because the txtTitle.Text returns the string.
Kind Regards
shakina
Member
2 Points
1 Post
Re: textbox databinding to a Sql Database.
Sep 25, 2008 02:01 AM|LINK
please help me with textbox data binding to ms accessto display a row in textboxes
ASP.NET