如何将dropdownlist与sql数据库连接? [英] How to connect dropdownlist with sql database ?

查看:139
本文介绍了如何将dropdownlist与sql数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有两个下拉列表和一个文本框,其中

dpl.1>城市名称
dpl.2>城市名称
txtbox.1>公里获取


由于它已连接到sql数据库,所以我希望dropdownlist中的数据(城市名称)直接从dropdownlist中的数据库中选择,并快速回答txtbox中的kms.


有什么天才可以解决这些问题....!

hey, i have two dropdownlist and one textbox in which

dpl.1> name of city
dpl.2> name of city
txtbox.1>kilometer to get


As it is connected to sql database , so i wish that the data(name of city) in dropdownlist directly select from database in both dropdownlist and quick answer the kms in txtbox.


Is there any genius , who can solve these problem....!

推荐答案

cmd.commondtext =从CityMast中选择公里,其中City1 = drop1和city2 = drop2"
txetbox.text = cme.executesaclar().tostring();
cmd.commondtext = "Select kilometer From CityMast Where City1=drop1 and city2=drop2"
txetbox.text=cme.executesaclar().tostring();


您还可以尝试以下操作:
使Both Dropdown Autopost Back Proprty = True ..

页面加载事件:
You also Try this:
make Both Dropdown Autopost Back Proprty = True..

on Page Load Event :
if (!IsPostBack)
{
         FillDropDownList();
         FillDropDownList1();
}


The Function is :

 private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("select city from tablename ",con);
        myda.Fill(ds);
        drop_city.DataSource = ds;
        drop_city.DataValueField = "city";
        drop_city.DataBind();
    }


private void FillDropDownList1()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("select city from tablename ",con);
        myda.Fill(ds);
        drop_city1.DataSource = ds;
        drop_city1.DataValueField = "city";
        drop_city1.DataBind();
    }



双击drop_city1下拉



Double Click on drop_city1 Drop down

string str="Select km from tablename where city =''"+drop_city.SelectedItem.Value+"'' and city1=''"+drop_city1.SelectedItem.Value+"''";
        SqlDataAdapter ad = new SqlDataAdapter(str,connection object);
        DataSet ds = new DataSet();
        ad.Fill(ds);

        if (ds.Tables[0].Rows.Count != 0)
        {
            Textbox1.Text = ds.Tables[0].Rows[0][0].ToString();
        }




如有疑问,请发布....




if any query, please post it....


使用数据集获取值(nameofCity,km等),然后使用填充组合框,如:

cmb_city.Items.Add(dataset.Tables [0] .Rows [i] ["column_name"]);

:)很高兴为您提供帮助


--TK-
use dataset for fetch value(nameofCity,km, etc...) and then use fill combobox like that:

cmb_city.Items.Add(dataset.Tables[0].Rows[i]["column_name"]);

:)happy to help


--TK--


这篇关于如何将dropdownlist与sql数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆