retrive data from sql server 2000 via dropdown list in label [Resolved]

Posted by Saanjhi under ADO.NET on 10/2/2013 | Points: 10 | Views : 2283 | Status : [Member] | Replies : 3
making a search panel want to show data in labels and text box via dropdownlist box and data retrieved from sql server 2000




Responses

Posted by: Bandi on: 10/3/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Populate a Label and TextBox control based from the seselected value from the DropDownList :

private void GetData(string ID)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM YourTable WHERE ID= @ID", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@ID",ID);
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
Label1.Text = dt.Rows[0]["ColumnName2"].ToString();
}
connection.Close();
}


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


Reference:
http://forums.asp.net/t/1537443.aspx
http://forums.asp.net/t/1212831.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Saanjhi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saanjhi on: 10/3/2013 [Member] Starter | Points: 25

Up
0
Down
and that data in grid view ?? instead of labels or text box???

Saanjhi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/3/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Look into the following Sample:

Here I took one drop downlist with values 10, 20, 30, 40 and 50 as department IDs and also one GridView for data binding based on DropDownList selected value

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewWithDropDownList.aspx.cs" Inherits="TextBoxValidation.GridViewWithDropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" AutoPostBack="true" ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="10" />
<asp:ListItem Text="20" />
<asp:ListItem Text="30" />
<asp:ListItem Text="40" />
<asp:ListItem Text="50" />
</asp:DropDownList>
<asp:GridView runat="server" ID="gdvEmp"/>
</div>
</form>
</body>
</html>


aspx.cs Code

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 TextBoxValidation
{
public partial class GridViewWithDropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData(DropDownList1.SelectedValue);
}
}
private void GetData(string ID){
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("Data source=XXXX ; Initial Catalog=study ; Integrated Security=true");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Employees WHERE Department_ID= @ID", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@ID",ID);
//sqlDa.Fill(dt);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
gdvEmp.DataSource = ds;
gdvEmp.DataBind();

connection.Close();
}

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


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Saanjhi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response