0

我正在尝试从 ProductDB(数据库)创建搜索,我希望用户搜索的主要列是 Material_No 和 Product_Line。

到目前为止,我有以下内容:

下拉列表

<asp:DropDownList ID="DropDownList" runat="server" Height="16px" 
        onclick="SearchButton_Click" Width="144px" 
        AutoPostBack="True">
        <asp:ListItem>Please select...</asp:ListItem>
        <asp:ListItem Value="0">Material No</asp:ListItem>
        <asp:ListItem Value="1">Product Line</asp:ListItem>
    </asp:DropDownList>

文本框

<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">
</asp:TextBox>

搜索按钮

<asp:Button ID="SearchButton" runat="server" Text="Search" 
onclick="SearchButton_Click" />

所以我想做的是当用户在单击搜索按钮后键入材料编号或产品线时选择材料编号或产品线时,结果应该以网格格式或类似的形式显示,如果他只是单击搜索没有选择所有结果应该显示的任何内容。

这是我到目前为止所做的。

旧代码

protected void SearchButton_Click(object sender, EventArgs e)
    {
        string  Selectedvalue = DropDownList.SelectedItem.Value;
        if (DropDownList.SelectedItem.ToString() == "Material No")
        {
            MessageBox.Show("Material No selected");
            string textbox = TextBox1.Text;
            MessageBox.Show(textbox.ToString());

            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");

            DataSet dsData = new DataSet();

            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "SELECT * FROM ProductDB WHERE Material_No ='" + TextBox1.Text + "'";
            cmd.Connection = conn;

            conn.Open();

            SqlDataAdapter da = new SqlDataAdapter("", conn);

            SqlCommandBuilder cmdBldr = new SqlCommandBuilder(da);
            da.Fill(dsData, TextBox1.Text);

            MessageBox.Show("Connection Successful");
            conn.Close(); 
        }
        else
        {
            MessageBox.Show("Product Line selected");
        }


    }

新代码

private SqlConnection conn;
private SqlDataAdapter daMaterial;
private SqlDataAdapter daProduct;

private SqlCommand cmdMaterial;
private SqlCommand cmdProduct;

private SqlParameter paramMaterial;
private SqlParameter paramProduct;

private DataSet dsMaterial;
private DataSet dsProduct;
private DataGrid dgMaterial;
private DataGrid dgProduct;

private const string tableNameMaterial = "Material_No";
private const string tableNameProduct = "Product_Line";
enter code here
protected void SearchButton_Click(object sender, EventArgs e)
    {
        string Selectedvalue = DropDownList.SelectedItem.Value;
        if (DropDownList.SelectedItem.ToString() == "Material No")
        {
            //MessageBox.Show("Material No. Selected");
            string textbox = TextBox1.Text;
            //MessageBox.Show(textbox.ToString());

            conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
            dsMaterial = new DataSet(); 
            daMaterial = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Material_No = @Material_No", conn);
            daMaterial.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Material_No = @Material_No";
            paramMaterial = new SqlParameter();
            paramMaterial.ParameterName = "@Material_No";
            paramMaterial.Value = TextBox1.Text;

            daMaterial.SelectCommand = cmdMaterial;
            cmdMaterial.Parameters.Add("@Material_No", SqlDbType.BigInt).Value = TextBox1.Text;

            daMaterial.Fill(dsMaterial, tableNameMaterial);              

            //MessageBox.Show("Connection Successful");
            conn.Close();
        }
        else
        {
            //MessageBox.Show("Product Line selected");
            string textbox = TextBox1.Text;
            //MessageBox.Show(textbox.ToString());


            conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
            dsProduct = new DataSet();
            daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = @Product_Line", conn);
            daProduct.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = @Product_Line";
            paramProduct = new SqlParameter();
            paramProduct.ParameterName = "@Product_Line";
            paramProduct.Value = TextBox1.Text;

            daProduct.SelectCommand = cmdProduct;
            cmdProduct.Parameters.Add("@Product_Line", SqlDbType.VarChar, 50).Value = TextBox1.Text;
            conn.Open();
            daProduct.Fill(dsProduct, tableNameProduct);

            //MessageBox.Show("Connection Successful");
            conn.Close();
        }


    }

我收到错误消息“对象引用未设置为对象的实例。”

有人可以检查Parameter使用是否正确SqlDataAdapter

4

3 回答 3

1
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string Query = string.Empty;
        try
        {
            if (sqlCon.State == ConnectionState.Closed)
            {
                sqlCon.Open();
            }
            if (DropDownList1.SelectedValue.ToString() == "Name")
            {
                Query = "select * from tbl_Employee where Name Like '" + txtSearchName.Text +  "%'";
            }
            else if (DropDownList1.SelectedValue.ToString() == "Designation")
            {
                Query = "select * from tbl_Employee where Designation Like '" + txtSearchName.Text + "%'";
            }
            SqlDataAdapter sqlDa = new SqlDataAdapter(Query, sqlCon);
            DataSet Ds = new DataSet();
            sqlDa.Fill(Ds);
            dgvEmployee.DataSource = Ds;
            dgvEmployee.DataBind();
        }
        catch (Exception ex)
        {

            HttpContext.Current.Response.Write("<script>alert('wfrmGrid: 11')</script>" + ex.Message);
        }

    }
于 2013-12-01T06:41:10.283 回答
0

一些事情:

daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = @Product_Line", conn);

创建一个新的并使用 and对其进行SqlDataAdapter初始化,因此以下几行是多余的并且本质上容易出错:SelectCommandCommandTextConnection

  1. daProduct.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = @Product_Line";
  2. daProduct.SelectCommand = cmdProduct;

第二条指令甚至用新的CommandText 并且Connection从未使用过的方式覆盖了第一条指令。

paramProduct = new SqlParameter();

而不是使用这个无参数的构造函数,我会使用AddWithValue或者Parameters.Add不太容易出错(例如,你没有提供类型)。

cmdProduct.Parameters.Add( ....

现在您正在使用我建议的方法,但从未使用过paramProduct. 您也永远不会处理非托管资源(例如,在出现错误时连接保持打开状态),因此请使用using-statement。顺便说一句,如果你使用 aDataAdapter你甚至不需要打开/关闭连接,那是在DataAdapter.Fill.

对不起,但你的代码是一团糟。我不会摆弄你的,而是提供一个应该可以工作的干净版本。

....
else
{
    using(var con =  new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
    using (var daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = @Product_Line", con))
    {
        daProduct.SelectCommand.Parameters.Add("@Product_Line", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        dsProduct = new DataSet();
        daProduct.Fill(dsProduct, "Product_Line");
    }
}
于 2013-01-30T09:32:40.190 回答
0

以下代码正在工作:

private const string tableNameMaterial = "Material_No";
private const string tableNameProduct = "Product_Line";

protected void SearchButton_Click(object sender, EventArgs e)
    {
        string Selectedvalue = DropDownList.SelectedItem.Value;
        if (DropDownList.SelectedItem.ToString() == "Material No")
        {
            //MessageBox.Show("Material No. Selected");
            string textbox = TextBox1.Text;
            //MessageBox.Show(textbox.ToString());

            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;

            cmd.CommandText = "SELECT * FROM ProductDB WHERE Material_No = @Material_No";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add("@Material_No", SqlDbType.BigInt);
            cmd.Parameters["@Material_No"].Value = TextBox1.Text;

            SqlDataAdapter daMaterial = new SqlDataAdapter();
            daMaterial.SelectCommand = cmd;

            DataSet dsMaterial = new DataSet();
            conn.Open();
            daMaterial.Fill(dsMaterial, tableNameMaterial);

            //MessageBox.Show("Connection Successful");
            GridView1.DataSource = dsMaterial;
            GridView1.DataBind();

            conn.Close();
        }
        else
        {
            //MessageBox.Show("Product Line selected");
            string textbox = TextBox1.Text;
            //MessageBox.Show(textbox.ToString());

            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;

            cmd.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = @Product_Line";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add("@Product_Line", SqlDbType.VarChar);
            cmd.Parameters["@Product_Line"].Value = TextBox1.Text;

            SqlDataAdapter daProduct = new SqlDataAdapter();
            daProduct.SelectCommand = cmd;

            DataSet dsProduct = new DataSet();
            conn.Open();
            daProduct.Fill(dsProduct, tableNameProduct);

            //MessageBox.Show("Connection Successful");
            GridView1.DataSource = dsProduct;
            GridView1.DataBind();

            conn.Close();
        }


    }

现在,我需要搜索框来自动完成数据库中的字母......如果有人有任何建议,请分享。

谢谢

于 2013-01-30T11:44:07.797 回答