Hi, I am quite new to ASP.Net and am having a problem with a dropdownlist. The user will choose a site number from the dropdownlist. This should then automatically populate the textboxes called site_name, site_address, site_county and site_postcode with data from the following SQL command:
SELECT site_num, site_name, site_address, site_county, site_postcode FROM SCSite WHERE Site_Num =
I believe that I should be using OnSelectedIndexChanged, but am stuck with the C# code behind to achieve the population of the textboxes. Below is the code for the relevant section of the main page.
Any help would be most grateful!
Please Select |
Site Name |
|
Address |
|
County |
|
Postcode |
|
Satyapriya NayakPosted Jun 12, 2014, 6:05 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
Top_Site_NumDropDown.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
con.Open();
str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Top_Site_NumDropDown.Items.Add(reader["Site_Num"].ToString());
}
reader.Close();
con.Close();
}
}
private void clear()
{
Top_Site_NameTextBox.Text = "";
Top_Site_AddressTextBox.Text = "";
Top_Site_CountyTextBox.Text = "";
Top_Site_PostcodeTextBox.Text = "";
}
protected void Top_Site_NumDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
clear();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite where Site_Num='" + Top_Site_NumDropDown.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Top_Site_NameTextBox.Text = reader["Site_Name"].ToString();
Top_Site_AddressTextBox.Text = reader["Site_Address"].ToString();
Top_Site_CountyTextBox.Text = reader["Site_County"].ToString();
Top_Site_PostcodeTextBox.Text = reader["Site_Post_Code"].ToString();
}
reader.Close();
con.Close();
}
}
}
ChrisPosted Jun 12, 2014, 6:42 AM
ChrisPosted Jun 12, 2014, 5:07 AM
ASPX PAGE
<%@ Page Title="Add New Project" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="ProjectReviewAdd.aspx.cs" Inherits="Project_Review.ProjectReviewAdd" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
CS CODE BEHIND
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["SqlDataSourceSite"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
Top_Site_NumDropDown.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
Top_Site_NumDropDown.Items.Add("Site_Num");
con.Open();
str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Top_Site_NumDropDown.Items.Add(reader["Site_Num"].ToString());
}
reader.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite where id='" + Top_Site_NumDropDown.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Top_Site_NameTextBox.Text = reader["Site_Name"].ToString();
}
reader.Close();
con.Close();
}
}
Satyapriya NayakPosted Jun 12, 2014, 12:19 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_click_data_in_textbox
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList1.Items.Add("Choose id");
con.Open();
str = "select * from test";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["id"].ToString());
}
reader.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from test where id='" + DropDownList1.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
TextBox1.Text = reader["name"].ToString();
}
reader.Close();
con.Close();
}
}
}
Khan Abrar AhmedPosted Jun 11, 2014, 12:43 PM