如何在Dropdownlist中选择forignkey时在网格中显示相关信息? [英] How to display related information in a Grid upon selection of a forignkey from Dropdownlist?

查看:56
本文介绍了如何在Dropdownlist中选择forignkey时在网格中显示相关信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面提到的代码,根据下拉列表中的外键选择从gridview中的表中检索数据。但是发生了SQL异常。无效处理。



I am using the below mentioned code to retrieve the data from the table in gridview on the bases of a foreign key selection from drop down list. But a SQL exception occurs. Invalidaction.

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;
using System.Configuration;

public partial class About : Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillEmpDropdownList();
        }
    }
    protected void FillEmpDropdownList()
    {
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adp = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
            cmd = new SqlCommand("Select * from ForexRateNew order by CurrencyFrom ASC", con);
            adp.SelectCommand = cmd;
            adp.Fill(dt);
            Currencies.DataSource = dt;
            Currencies.DataTextField = "CurrencyFrom";
            Currencies.DataValueField = "CurrencyFrom";
            Currencies.DataBind();
            Currencies.Items.Insert(0, "-- Select --");
            //OR    ddlEmpRecord.Items.Insert(0, new ListItem("Select Emp Id", "-1"));
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
        finally
        {
            cmd.Dispose();
            adp.Dispose();
            dt.Clear();
            dt.Dispose();
        }
    }

    protected void Currencies_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string CurrencyFrom = Convert.ToString(Currencies.SelectedValue);
            BindEmpGrid(CurrencyFrom);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
    }
    private void BindEmpGrid(string CurrencyFrom)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter();
        try
        {
            SqlCommand cmd = new SqlCommand("Select *  from ForexRateNew where CurrencyFrom=" + CurrencyFrom + " ", con);
            adp.SelectCommand = cmd;
            adp.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                RatesGrid.DataSource = dt;
                RatesGrid.DataBind();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
            adp.Dispose();
        }
    }
}



表结构

---------- ------



ForexRateNew

------------

CurrencyFrom varchar(20)FK,

CurrencyTo varchar(20),

SellRate十进制(8,8),

BuyRate十进制(8) ,8)


Table Structure
----------------

ForexRateNew
------------
CurrencyFrom varchar(20) FK,
CurrencyTo varchar(20),
SellRate Decimal(8,8),
BuyRate Decimal(8,8)

推荐答案

使用参数

use parameters
SqlCommand cmd = new SqlCommand("Select *  from ForexRateNew where CurrencyFrom=@CurrencyFrom", con);
cmd.Parameters.AddWithValue("@CurrencyFrom", CurrencyFrom);
adp.SelectCommand = cmd;
adp.Fill(dt);


这篇关于如何在Dropdownlist中选择forignkey时在网格中显示相关信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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