• pwalter83 (1/30/2013)


    Sean Lange (1/29/2013)


    pwalter83 (1/29/2013)


    Thanks Sean,

    I have come to a point where I need to add the sql code within this:

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

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub

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

    The SQL code would be as you suggested- "select Columns from MyTable where Col1 = Combobox 1 and Col2 = Combobox 2" but I dont know what vb.net syntax I need to enter this in.

    Any suggestions would be welcome. Thanks,

    Paul

    I would make two suggestions. First give your button a name that means something. Button1 is like calling a column Column1 in sql, maybe something like btnSearch. Not naming your controls is a habit that is really tough to break unless you start early.

    Secondly, don't run that sql code. Instead execute a stored procedure and pass the combobox values as parameters. Look up the syntax with google/bing/whatever. There are a number of ways to query data from the database with .NET.

    Slowly but not steadily, I have reached here now:

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

    using System;

    using System.Data;

    using System.Collections.Generic;

    using System.Data.SqlClient;

    using System.Globalization;

    using System.Text.RegularExpressions;

    using System.Security.Principal; // here is the security namespace you need

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    protected void Button1_Click(object sender, EventArgs e)

    {

    DataTable dt = new DataTable();

    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");

    connection.Open();

    SqlCommand sqlCmd = new SqlCommand("SELECT Office_cd,Trade_cd, system_name, interface_direction, last_update_dt FROM header WHERE office_cd = @Value1 AND trade_cd =@Value2", connection);

    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@Value1", Office_cd.SelectedItem.Text);

    sqlCmd.Parameters.AddWithValue("@Value2", trade_cd.SelectedItem.Text);

    sqlDa.Fill(dt);

    if (dt.Rows.Count > 0)

    {

    TextBox1.Text = dt.Rows[0]["Office_cd"].ToString();

    //Where ColumnName is the Field from the DB that you want to display

    TextBox2.Text = dt.Rows[0]["Trade_cd"].ToString();

    }

    connection.Close();

    }

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

    However, I am now getting this error on Datatable- Error 1 The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?) . Inspite of including 'using System.Data;' reference, I get this error, would you know how it can be corrected ?

    Thanks

    I am able to display the values in the gridview now but it shows up all the values that are in the table. My requirement is to only show data based upon the values selected by the user from the 2 drop down lists and then pressing the 'Find' button.

    The code in the click event of the 'Find' button seems to be okay to display the filtered values but it still doesnt work. Please find the code attached.using System;

    using System.Data;

    using System.Configuration;

    using System.Collections.Generic;

    using System.Data.SqlClient;

    using System.Globalization;

    using System.Text.RegularExpressions;

    using System.Security.Principal; // here is the security namespace you need

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    namespace GISA

    {

    public partial class _Default : System.Web.UI.Page

    {

    private string GetConnectionString()

    {

    return ConfigurationManager.ConnectionStrings["GISAConnectionString"].ConnectionString;

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

    DataTable dt = new DataTable();

    SqlConnection connection = new SqlConnection(GetConnectionString());

    connection.Open();

    SqlCommand sqlCmd = new SqlCommand("SELECT Office_cd,Trade_cd, system_name, interface_direction, last_update_dt FROM header WHERE office_cd = @Value1 AND trade_cd =@Value2", connection);

    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@Value1", Office_cd.SelectedItem.Text);

    sqlCmd.Parameters.AddWithValue("@Value2", trade_cd.SelectedItem.Text);

    sqlDa.Fill(dt);

    if (dt.Rows.Count > 0)

    {

    GridView1.DataSource = dt;

    GridView1.DataBind();

    }

    connection.Close();

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

    }

    protected void Button3_Click(object sender, EventArgs e)

    {

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

    }

    protected void Office_cd_SelectedIndexChanged(object sender, EventArgs e)

    {

    }

    protected void Office_cd0_SelectedIndexChanged(object sender, EventArgs e)

    {

    }

    protected void Find_Command(object sender, CommandEventArgs e)

    {

    }

    }

    }

    Thanks.