简体   繁体   English

C#数据库检索

[英]C# database retrieval

Again I can't get the answer for my query. 同样,我无法获得查询的答案。 I have written the code to retrieve the data from the database and load it into a textbox by selecting the dropdown list. 我已经编写了代码来从数据库中检索数据,并通过选择下拉列表将其加载到文本框中。 If I have selected my data in dropdown list it will not give the answer exactly, instead it'll show selected index 0. I mean it'll show "-Select-". 如果在下拉列表中选择了我的数据,它将不会给出确切的答案,而是显示所选索引0。我的意思是,它将显示“ -Select-”。 there is nothing done into the textbox. 在文本框中没有完成任何操作。 I have checked my asp page for OnSelectedIndex and AutoPostBack=True. 我已经检查了我的asp页的OnSelectedIndex和AutoPostBack = True。 Please correct me.. Here is my code: 请纠正我。这是我的代码:

//This is for retrieval of data to dropdown list.
 public void engi()
    {
        DropDownList1.Items.Clear();
        ListItem l = new ListItem();
        l.Text = "-Select-";
        DropDownList1.Items.Add(l);
        DropDownList1.SelectedIndex = 0;
        Conhr.Open();
        SqlCommand cmd = new SqlCommand("select EmployeeName from tbl_EmploeeDetails where Designation like('%Engineer') ", Conhr);
        //SqlCommand cmd=new SqlCommand("select  Sitecode from MRsite where Sitealiasname in (select Componetcode from tbl_Component where Sitecode='" + TextBox1.Text.Trim() + "'");
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            ListItem n = new ListItem();
            n.Text = dr["EmployeeName"].ToString().Trim();
            DropDownList1.Items.Add(n);
        }
        dr.Close();
        Conhr.Close();
    }

//This is for retrieval data for text box by selecting DropDown List
 public void des()
    {
        Conhr.Open();
        string s3;
        s3 = "select Designation from tbl_EmploeeDetails where EmployeeName='" + DropDownList1.SelectedItem.Text + "'";
        SqlCommand c3 = new SqlCommand(s3, Conhr);
        SqlDataReader d3;
        d3 = c3.ExecuteReader();
        while (d3.Read())
        {
            TextBox1.Text = d3["Designation"].ToString().Trim();

        }
        d3.Close();
        Conhr.Close();
    }
//Called the method for data retrieval for Textbox

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        des();

    }

I'm glad that you've posted your code (good!) - but we need more information. 很高兴您发布了代码(很好!)-但我们需要更多信息。

SUGGESTION: 建议:

1) query your database directly (eg using the MSSQL GUI). 1)直接查询数据库(例如,使用MSSQL GUI)。 Find a "select" statement that works. 找到一个有效的“选择”语句。

2) step into the debugger. 2)进入调试器。 Print your SQL (eg "s3") just before you query. 在查询之前打印您的SQL(例如“ s3”)。

3) compare the "good query" with the "failing" query. 3)比较“良好查询”与“失败”查询。

The problem could be as simple as a spelling error: 问题可能像拼写错误一样简单:

-- Maybe you meant "tbl_EmployeeDetails" here?
s3 = 
  "select Designation from tbl_EmploeeDetails where EmployeeName='" +       
  DropDownList1.SelectedItem.Text + "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM