title: Search engine by Drop Down List selection and text box entry searches Database for the entry and displays in aspx
body: <p>I am trying to create a search from ProductDB(database), the main columns I would like the user to search is Material_No and Product_Line.</p>
  
  <p>So far, I have the following:</p>
  
  <p><strong>Drop Down List</strong>:</p>
  
  <pre><code>&lt;asp:DropDownList ID="DropDownList" runat="server" Height="16px" 
          onclick="SearchButton_Click" Width="144px" 
          AutoPostBack="True"&gt;
          &lt;asp:ListItem&gt;Please select...&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="0"&gt;Material No&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="1"&gt;Product Line&lt;/asp:ListItem&gt;
      &lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p><strong>TextBox</strong>:</p>
  
  <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"&gt;
  &lt;/asp:TextBox&gt;
  </code></pre>
  
  <p><strong>Search Button</strong>:</p>
  
  <pre><code>&lt;asp:Button ID="SearchButton" runat="server" Text="Search" 
  onclick="SearchButton_Click" /&gt;
  </code></pre>
  
  <p>So I am trying to do is when the user chooses either Material No or Product Line when he types the Material No or Product Line after clicking the search button, the result should show either in grid format or something similar, and if he just clicks search without choosing anything all the result should show.</p>
  
  <p>Here is what I have done so far.</p>
  
  <p><strong>Old Code</strong>:</p>
  
  <pre><code>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");
          }
  
  
      }
  </code></pre>
  
  <p><strong>New Code</strong>:</p>
  
  <pre><code>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();
          }
  
  
      }
  </code></pre>
  
  <p>I am getting an error "Object reference not set to an instance of an object."</p>
  
  <p>Can someone check whether the <code>Parameter</code> use is correct with the <code>SqlDataAdapter</code></p>