Fetching Data from existing tables into the textboxes

Last post 07-04-2009 6:17 AM by vinz. 7 replies.

Sort Posts:

  • Fetching Data from existing tables into the textboxes

    07-03-2009, 3:54 AM
    • Member
      13 point Member
    • Learning++
    • Member since 07-02-2009, 5:50 AM
    • Posts 78

    Hello Friends

    Please help me on this. I need to fetch data into empty text boxes from a database. Lets say, I have a vendor general info. table, where Vendor ID. is the primary key. first i have to choose a Vendor  ID( may be dropdown list, which is also coming from database). As soon as i choose a ID, the values in the other user controls(text boxes) on the page, like Vendor First Name, Last Name are updated from the database.

     

     

    Thanks in Advance !

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 6:50 AM
    Answer
    • All-Star
      91,064 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,699
    • TrustedFriends-MVPs

    Here's an example using the ADO.NET way with DataTable:

    C#:

    private void GetData(string vendorID)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT * FROM TABLE1 WHERE VendorID= @VendorID", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlCmd.Parameters.AddWithValue("@VendorID",vendorID);
        sqlDa.Fill(dt);
        if (dt.Rows.Count > 0)
        {
               TextBox1.Text = dt.Rows[0]["ColumnName1"].ToString(); //Where ColumnName is the Field from the DB that you want to display
        }
            connection.Close();
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
            GetData(DropDownList1.SelectedItem.Value);
    } 



    VB.NET:

    Private Sub GetData(ByVal vendorID As String)
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
        connection.Open()
        Dim sqlCmd As New SqlCommand("SELECT * FROM TABLE1 WHERE VendorID= @VendorID", connection)
        Dim sqlDa As New SqlDataAdapter(sqlCmd)
        sqlCmd.Parameters.AddWithValue("@VendorID", vendorID)
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then
                'Where ColumnName is the Field from the DB that you want to display
            TextBox1.Text = dt.Rows(0)("ColumnName1").ToString()
        End If
        connection.Close()
    End Sub
    
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        GetData(DropDownList1.SelectedItem.Value)
    End Sub
    



    Best Regards,
    Vincent Maverick Durano
    SDE|Microsoft MVP - ASP/ASP.NET

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 6:58 AM
    • Member
      13 point Member
    • Learning++
    • Member since 07-02-2009, 5:50 AM
    • Posts 78

    Hello Vinz

    Thanks for replying !

    I am using Webforms. How can i apply this in it.

     

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 7:14 AM
    • All-Star
      91,064 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,699
    • TrustedFriends-MVPs

    Learning++:
    I am using Webforms. How can i apply this in it.

    You can put the codes that I have provided above in the Code Behind ASPX.CS or ASPX.VB file  of the WebForm (ASPX)...

    If you are new to ASP.NET then I would suggest you to start looking at these links below:

    Get Started:   http://www.asp.net/get-started/
    Learn:            http://www.asp.net/learn/
    Videos:          http://www.asp.net/learn/videos/
    Data Access:  http://www.asp.net/learn/data-access/



    Best Regards,
    Vincent Maverick Durano
    SDE|Microsoft MVP - ASP/ASP.NET

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 7:34 AM
    • Member
      13 point Member
    • Learning++
    • Member since 07-02-2009, 5:50 AM
    • Posts 78

     

    protected void ddldropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetData(ddldropdownlist1.SelectedItem.Value);
        } 


    Hello vinz

    Can you please tell me where to initialise ddldropdownlist1. Is it converting the fields in the column into the dropdown. If it is so, where to initialise it.

    below is my code:

    .aspx :

    <asp:TextBox ID="txtVendorID"  MaxLength="100" runat="server" SkinID="TextBox" ></asp:TextBox>
     
    .cs :
     
    private void GetData(string vendorID)
        {
            DataTable dt = new DataTable();
            using (SqlConnection Connection = new SqlConnection(@"Data Source=.;Initial Catalog=VendorDatabaseAdministration;User Id=ADMIN;pwd=0000"))
            {
                Connection.Open();
                SqlCommand sqlCmd = new SqlCommand("SELECT * FROM VendorGeneralDescription WHERE [Vendor ID]= @VendorID", Connection);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
                sqlCmd.Parameters.AddWithValue("@VendorID", txtVendorID.Text);
                sqlDa.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    txtVendorID.Text = dt.Rows[0]["[Vendor ID]"].ToString(); //Where ColumnName is the Field from the DB that you want to display
                }
                Connection.Close();
            }
        }
    
        protected void ddldropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetData(ddldropdownlist1.SelectedItem.Value);
        } 


     

     

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 11:46 AM
    Answer
    • Star
      8,670 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,498

     You can do this easy with no code at all!!!

    Recently, I answered on a similir question with a a complete example:

    http://forums.asp.net/t/1441766.aspx#3267613

     

  • Re: Fetching Data from existing tables into the textboxes

    07-03-2009, 12:03 PM
    Answer
    • All-Star
      59,423 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,516

     See if you can get something from here

    It has both Dropdown and TextBox

    http://www.aspsnippets.com/post/2009/06/07/DataBinding-DropDownList-Label-and-Textbox-Controls-in-ASPNet.aspx

  • Re: Fetching Data from existing tables into the textboxes

    07-04-2009, 6:17 AM
    Answer
    • All-Star
      91,064 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,699
    • TrustedFriends-MVPs

    Learning++:

    Hello vinz

    Can you please tell me where to initialise ddldropdownlist1. Is it converting the fields in the column into the dropdown.

    Just grab a DropDownList from the Visual Studio toolbox and then put it in the WebForm.. from there you can generate the SelectedIndexChanged event by double clicking on the DropDownList..

    Also, I would suggest you to refer to the links that I have provided above and I'm sure you will get some useful information there..

    You may check this example below:

    Binding DropDownList, ListBox and CheckBoxList Control the ADO.NET way.

    Best Regards,
    Vincent Maverick Durano
    SDE|Microsoft MVP - ASP/ASP.NET

    "Code,Beer and Music ~ my way of being a programmer"

Page 1 of 1 (8 items)