textbox databinding to a Sql Database.

Rate It (1)

Last post 10-22-2009 3:25 AM by gaurabchatterjee. 25 replies.

Sort Posts:

  • textbox databinding to a Sql Database.

    11-06-2006, 2:24 PM
    • Member
      146 point Member
    • mikedopp
    • Member since 10-09-2006, 9:17 PM
    • SLC
    • Posts 19

    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 

    http://weblogs.asp.net/mikedopp
  • Re: textbox databinding to a Sql Database.

    11-06-2006, 9:00 PM
    • Star
      12,126 point Star
    • shados
    • Member since 07-07-2006, 11:24 PM
    • Posts 2,202

    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 :) 

  • Re: textbox databinding to a Sql Database.

    11-14-2006, 2:11 PM
    • Member
      146 point Member
    • mikedopp
    • Member since 10-09-2006, 9:17 PM
    • SLC
    • Posts 19

    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 

    http://weblogs.asp.net/mikedopp
  • Re: textbox databinding to a Sql Database.

    11-14-2006, 5:20 PM
    • Star
      12,126 point Star
    • shados
    • Member since 07-07-2006, 11:24 PM
    • Posts 2,202

    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? 

  • Re: textbox databinding to a Sql Database.

    11-17-2006, 10:05 AM
    • Member
      10 point Member
    • KayBell
    • Member since 07-08-2003, 12:22 PM
    • Posts 5

    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.Inserting

    e.Command.Parameters[

    "fname"].DefaultValue = txtFirstName.Text.ToString()

    End Sub

    End

    Class

    Thanks for any help.

    Kay
  • Re: textbox databinding to a Sql Database.

    11-17-2006, 3:27 PM
    • Star
      12,126 point Star
    • shados
    • Member since 07-07-2006, 11:24 PM
    • Posts 2,202
    If you're using VB, use parenthesis instead of brackets :)
  • Re: textbox databinding to a Sql Database.

    11-20-2006, 4:41 PM
    • Member
      10 point Member
    • KayBell
    • Member since 07-08-2003, 12:22 PM
    • Posts 5

    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

    Kay
  • Re: textbox databinding to a Sql Database.

    01-03-2007, 3:37 PM
    • Member
      146 point Member
    • mikedopp
    • Member since 10-09-2006, 9:17 PM
    • SLC
    • Posts 19

    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 

    http://weblogs.asp.net/mikedopp
  • Re: textbox databinding to a Sql Database.

    03-28-2007, 9:20 AM
    • Member
      59 point Member
    • alug
    • Member since 03-26-2007, 9:28 AM
    • Posts 20

    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

  • Re: textbox databinding to a Sql Database.

    09-24-2008, 10:01 PM
    • Member
      2 point Member
    • shakina
    • Member since 09-25-2008, 1:46 AM
    • Posts 1

     

    please help me with textbox data binding to ms access

    to display a row in textboxes
    Filed under:
  • Re: textbox databinding to a Sql Database.

    11-06-2008, 6:29 PM
    • Member
      2 point Member
    • begineer
    • Member since 11-06-2008, 11:18 PM
    • Posts 8

    can any body pls help me with this, textbox data binding to Access database.

    >.net begineer
  • Re: textbox databinding to a Sql Database.

    11-13-2008, 4:35 PM
    • Member
      146 point Member
    • mikedopp
    • Member since 10-09-2006, 9:17 PM
    • SLC
    • Posts 19

    I think you could do a oledb.InsertParameters["Title"].DefaultValue = txtTitle.Text.ToString(); or what ever the datasource is named.

    Check it out let me know.

    -Mike

    http://weblogs.asp.net/mikedopp
  • Re: textbox databinding to a Sql Database.

    03-16-2009, 12:36 AM

    Since you are using the SqlDataSource control, you actually don't need any code at all for what you want to do.  You can set the Parameter source to "Control" for each of the InsertParameters and that way the SqlDataSource will pull the values from the TextBox controls automatically.  To see an example click: http://www.csharpuniversity.com/2009/02/03/inserting-database-data-using-parameters-from-textbox-controls-with-the-aspnet-sqldatasource-control/

  • Re: textbox databinding to a Sql Database.

    03-16-2009, 1:40 PM
    • Member
      82 point Member
    • EFoo
    • Member since 01-15-2008, 9:06 PM
    • Lansing, MI
    • Posts 24

    If you are using an Access Database and have an AccessDataSource on your page, you should be able to use an Eval statement or codebehind to populate your textbox, depending on where your textbox is.  Could you please provide a little more info or sample code showing where your textbox is on your page.

    EFoo

    _____________________________________________________
    Please remember to click “Mark as Answer” on the post that helps you.
  • Re: textbox databinding to a Sql Database.

    03-23-2009, 3:28 PM
    • Member
      6 point Member
    • Jebusno2
    • Member since 03-23-2009, 7:06 PM
    • Posts 5

    I am trying to create a search page for my project which takes the values from two text boxes and uses an SQL datasource to search an SQL database and then display the results in a gridview control. The SQL statement i use is below.

    SELECT [IDno], [ItemName], [IType], [Price], [Quantity], [dateposted], Email, [phonenumber] FROM [Products] WHERE ((CONTAINS([ItemName], ?)) AND ([IType] = ?))

     It does not seem to work as when i tried to test the query it returned the error Null or empty full-text predicate and when i tried to use the query builder it returned an error message saying No value given for one or more required paramaters I do not know what to do. Please can someone help me?

Page 1 of 2 (26 items) 1 2 Next >