如何通过选择下拉列表将数据库值放入文本框中 [英] How to take database value into text box by selecting dropdownlist

查看:103
本文介绍了如何通过选择下拉列表将数据库值放入文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dropdownlist并且有从数据库中恢复的项目,我的问题是该项目选择项目值将显示在文本框中的任何一项?



< b>我尝试了什么:



 受保护  void  DropDownList1_SelectedIndexChanged( object  sender,EventArgs e)
{

SqlConnection con1 = new SqlConnection( 数据源= XENORIX8- PC;初始目录= xenorix;用户ID = ******;密码= *****);
SqlCommand cmd1 = new SqlCommand( select item_price from quantity where item_name =' + DropDownList1.SelectedItem.Text + ' ,con1);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
TextBox1.Text = dt1.Rows [ 0 ]。ToString();
}



这是否正确?如果错误请发送任何线索或代码

解决方案

在最后一行使用下面的代码



 TextBox1.Text = dt1.Rows [ 0 ] [ 0 ]。ToString(); 


试试这个



  protected   void  DropDownList1_SelectedIndexChanged( object  sender,EventArgs e) 
{

SqlConnection con1 = new SqlConnection( 您的连接字符串);
SqlCommand cmd1 = new SqlCommand( select item_price from quantity where item_name = @ item,con1);
cmd1.Parameters.Add( @ item,DropDownList1.SelectedItem.Text);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
if (dt1.Rows.Count > 0 // 验证以避免索引异常。
TextBox1.Text = dt1.Rows [ 0 ] [ item_price< /跨度>]的ToString(); // 定义列名
}





你应该知道 SQL注入 [ ^ ] 硬编码 SQl Statemetns


I am using dropdownlist and that has item retriveing from database, my question is that item select any one item that item value will display at text box?

What I have tried:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

    SqlConnection con1 = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=******;Password=*****");
    SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name='" + DropDownList1.SelectedItem.Text + "'", con1);
    SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
    DataTable dt1 = new DataTable();
    sda1.Fill(dt1);
    TextBox1.Text = dt1.Rows[0].ToString();
}


Is this correct or not?, If its wrong please send any clue or code

解决方案

use below code in last line

TextBox1.Text = dt1.Rows[0][0].ToString();


Try this

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
       {

           SqlConnection con1 = new SqlConnection("Your Connection string");
           SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name=@item", con1);
           cmd1.Parameters.Add("@item", DropDownList1.SelectedItem.Text);
           SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
           DataTable dt1 = new DataTable();
           sda1.Fill(dt1);
           if (dt1.Rows.Count > 0) // validate for avoiding index exception.
               TextBox1.Text = dt1.Rows[0]["item_price"].ToString(); // define the column name
       }



You should be aware of SQL Injection[^] when hardcoding the SQl Statemetns


这篇关于如何通过选择下拉列表将数据库值放入文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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