0

I have a DropDownList which contains the two columns one is CardCode and other is CardName and that is linked to a SQL database. It currently shows a list of CardCode + List of Cardname. I am trying to make it so that once a CardCode + CardName is selected from the two columns dropdownlist, multiple textboxes are automatically filled (such as CardNum, CntctPerson,ListNum etc). I am able to automatically fill the data from selecting only CardCode now I want to show the related row to the CardCode + CardName dropdown list, I do not know how to fill the other rows by selecting 2 column's dropdown list(CardCode + CardName) .How can i do this? Thanks In Advance

Here is my aspx.cs Code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace StackOver
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadOptions();
            }
        }
        protected void LoadOptions()
        {
            DataTable CardCode = new DataTable();

            SqlConnection connection = new SqlConnection("my connection string");
            using (connection)
            {
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT CardCode,CardName, Address, CntctPrsn FROM OCRD", connection);

                adapter.Fill(CardCode);
                DropDownList1.DataValueField = "CardCode";
                DropDownList1.DataTextField = "CardCode";
                DropDownList1.DataBind();
            }
        }

       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {


            string selected = DropDownList1.SelectedItem.Value;
            SqlConnection connection = new SqlConnection("my connection string");
            using (connection)
            {
                SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD WHERE CardCode = @CardCode", connection);
                connection.Open();
                theCommand.Parameters.AddWithValue("@CardCode", selected);
                theCommand.CommandType = CommandType.Text;

                SqlDataReader theReader = theCommand.ExecuteReader();

                   if (theReader.Read())
                    {
                        // Get the first row
                      // theReader.Read();

                        // Set the text box values
                        this.TextBox1.Text = theReader.GetString(0);
                        this.TextBox2.Text = theReader.GetString(1);
                        this.TextBox3.Text = theReader.GetString(2);
                     //   this.TextBox3 = reader.IsDBNull(TextBox3Index) ? null : reader.GetInt32(TextBox3Index)
                        // GenreID = reader.IsDBNull(genreIDIndex) ? null : reader.GetInt32(genreIDIndex)
                        this.TextBox4.Text = theReader.GetString(3);
                        //  TextBox5.Text = theReader.GetString(4);
                        //  TextBox6.Text = theReader.GetString(5);
                        //  TextBox7.Text = theReader.GetString(6);

                    }
                    connection.Close();
                }

            }



       public object TextBox3Index { get; set; }
    }

}

And also this is my .aspx code

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="StackOver._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="CardCode" 
            DataValueField="CardName" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:myconnectionstring %>" 
            SelectCommand="SELECT [CardCode] + '----' + [CardName] as CardCode, CardName,[Address], [CntctPrsn] FROM [OCRD]">
        </asp:SqlDataSource>
    </h2>

           <br />
        <br />
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
    Business Partner Code :&nbsp; 
        <asp:TextBox ID="TextBox1" runat="server" Width="192px" ></asp:TextBox>
    </p>
    <p>
    Business Partner Name :
        <asp:TextBox ID="TextBox2" runat="server" Width="192px" ></asp:TextBox>
    </p>
    <p>
     Address :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <asp:TextBox ID="TextBox3" runat="server" Width="196px" ></asp:TextBox>
    </p>

    <p>
    Contact Person Name :&nbsp; 
        <asp:TextBox ID="TextBox4" runat="server" Width="196px" ></asp:TextBox>
    </p>

     <p>
        <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
    </p>
     <p>
        <asp:TextBox ID="TextBox6" runat="server" ></asp:TextBox>
    </p>
     <p>
        <asp:TextBox ID="TextBox7" runat="server" ></asp:TextBox>
    </p>
</asp:Content>
1
  • No @Kaushik Sir that was selecting only CardCode i did that but now i want to select 2 column's dropdown list that Contains CardCode and CardName Commented Nov 25, 2015 at 4:58

1 Answer 1

1

Code behind Code.

 protected void LoadOptions()
    {
        DataTable CardCode = new DataTable();

        SqlConnection connection = new SqlConnection("my connection string");
        using (connection)
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT CardCode,CardName, Address, CntctPrsn FROM OCRD", connection);

            adapter.Fill(CardCode);
            if (CardCode.Rows.Count > 0)
            {
                for (int i = 0; i < CardCode.Rows.Count; i++)
                {
                     id = CardCode.Rows[i]["CardCode"].ToString();
                     name = CardCode.Rows[i]["CardName"].ToString();
                     newName = id + " ---- " + name;
                     DropDownList1.Items.Add(new ListItem(newName,id));
                }
            }
        }
    }

.Aspx Code

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="StackOver._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Welcome to ASP.NET!
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
</h2>

       <br />
    <br />
<p>
    &nbsp;</p>
<p>
    &nbsp;</p>
<p>
Business Partner Code :&nbsp; 
    <asp:TextBox ID="TextBox1" runat="server" Width="192px" ></asp:TextBox>
</p>
<p>
Business Partner Name :
    <asp:TextBox ID="TextBox2" runat="server" Width="192px" ></asp:TextBox>
</p>
<p>
 Address :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="TextBox3" runat="server" Width="196px" ></asp:TextBox>
</p>

<p>
Contact Person Name :&nbsp; 
    <asp:TextBox ID="TextBox4" runat="server" Width="196px" ></asp:TextBox>
</p>

 <p>
    <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
</p>
 <p>
    <asp:TextBox ID="TextBox6" runat="server" ></asp:TextBox>
</p>
 <p>
    <asp:TextBox ID="TextBox7" runat="server" ></asp:TextBox>
</p>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alot Dear @Kaushik :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.