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>
</p>
<p>
</p>
<p>
Business Partner Code :
<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 :
<asp:TextBox ID="TextBox3" runat="server" Width="196px" ></asp:TextBox>
</p>
<p>
Contact Person Name :
<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>