Compile Errors Using SQLConnection

My syntax shows these errors:

The name 'command' does not exist
The name 'conn' does not exist

I am declaring both of these variables, why am I getting errors? Here is full syntax.

namespace SQLDataPull
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SQL.DataTable dtData = new SQL.DataTable();
        string conString = @"Server=ProdDev;Database=Test;Integrated Security=SSPI;";
        StringBuilder query = new StringBuilder();
        SQL.DataTable dtProducts = new SQL.DataTable();
        query.Append("SELECT Top 1 [saleID] FROM [dbo].[saleorderitems] ORDER BY [saleID] ASC");
        //Populating datatable1 with the saleID
        using (SqlConnection cn = new SqlConnection(conString))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(query.ToString(), cn))
                da.Fill(dtProducts);
        }
        //Iterating the saleid from datatable
        foreach (DataRow row in dtProducts.Rows)
        {
            using (SqlConnection conn = new SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;")
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT * FROM [dbo].[master] WHERE saleID = @saleID;";
                command.Parameters.Add("@saleID", SqlDbType.VarChar);
                command.Parameters["@saleID"].Value = row.Field<string>("saleID");
                command.Connection = conn;                    
                using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
                {
                    dataadapter1.Fill(dtData);
                }
            }
        }  
    }
}
}

Upvotes: 0

Views: 74

Answers (2)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48570

You forget closing brackets ) at 2 places

using (SqlConnection conn = 
       new SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;"))

and

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())

Upvotes: 1

Zein Makki
Zein Makki

Reputation: 30042

You forgot one ) at the end of the using statement:

using (SqlConnection conn = new 
             SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;")

With

using (SqlConnection conn = new 
              SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;"))

Also you need to do the same for:

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())

Upvotes: 0

Related Questions