我正在尝试从 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