scedsh8919
scedsh8919

Reputation: 151

C# ASP Web Form Logic

Could someone tell me what I'm doing wrong? I've tried to accomplish this in numerous different ways, but have not been able to. Without adding the parameters in, the form runs, but I need the parameters so that I can update records if it so evaluates. I may be off track, so any help is very appreciated.

For example, if a product code is entered and doesn't have a date already, the form should update the date with the current date/time. If the product code does have a date already, it should notify the user that the product has already shipped, else telling the user that the product is not in the database.

It evaluates by querying if there is a product code and if the date is null. If that evaluates to be true, then it should update that product code with a current timestamp in the date column. If that evaluates to be false, it checks to see if the product code exists in the table at all. If it does and the date column is not null, it reports that the product has already shipped, else, it reports that the product doesn't exist in the database.

Without the following parameters, it runs fine, providing the correct responses but, of course, it doesn't ever call to update a record.

command2.Parameters.AddWithValue("@Value1", TextBox1.Text);
command2.Parameters.AddWithValue("@Value2", DateTime.Now);

With these parameters added in, I get an error stating the "The name 'command2' does not exist in the current context. But, I only get this error one. Sorry if my code is way out of line. Thanks in advance for your help!

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private string GetConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            try
            {
                connection.Open();

                string sql = @"SELECT PRODUCT_ID from PRODUCTS where PRODUCT_ID = " + TextBox1.Text + " and DATE is null";

                using(SqlCommand command = new SqlCommand(sql, connection))
                {
                    using(SqlDataReader reader = command.ExecuteReader())
                    {
                        if(reader.HasRows)
                        {
                            string sql2 = @"UPDATE [products] SET date=@Value2 where PRODUCT_ID=@Value1";
                            using (SqlCommand command2 = new SqlCommand(sql2, connection))
                                command2.Parameters.AddWithValue("@Value1", TextBox1.Text);
                                command2.Parameters.AddWithValue("@Value2", DateTime.Now);
                            pageBody.Attributes.Add("bgcolor", "#9aff8e");
                            Label1.Text = "Item " + TextBox1.Text + " Recorded!";
                            TextBox1.Text = "";
                        }
                        else
                        {
                            reader.Close();
                            string sql3 = @"SELECT PRODUCT_ID from PRODUCTS where PRODUCT_ID = " + TextBox1.Text + "";

                            using(SqlCommand command3 = new SqlCommand(sql3, connection))
                            {
                                using(SqlDataReader reader2 = command3.ExecuteReader())
                                {
                                    if (reader2.HasRows)
                                    {
                                        pageBody.Attributes.Add("bgcolor", "#fbff8e");
                                        Label1.Text = "Item " + TextBox1.Text + " Already Shipped!";
                                        TextBox1.Text = "";
                                    }
                                    else
                                    {
                                        pageBody.Attributes.Add("bgcolor", "#ff8e8e");
                                        Label1.Text = "Item " + TextBox1.Text + " Not Found!";
                                        TextBox1.Text = "";
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if(connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
        }
    }

}

Upvotes: 0

Views: 45

Answers (2)

Worthy7
Worthy7

Reputation: 1561

using (SqlCommand command2 = new SqlCommand(sql2, connection)) {
      command2.Parameters.AddWithValue("@Value1", TextBox1.Text);
      command2.Parameters.AddWithValue("@Value2", DateTime.Now);
}

Forgot your brackets.

Upvotes: 1

Oshi
Oshi

Reputation: 504

Put your parameters assignment inside a bracket and don't forget to call the execute method.

using (var command2 = new SqlCommand(sql2, connection)) 
{
    command2.Parameters.AddWithValue("@Value1", TextBox1.Text);
    command2.Parameters.AddWithValue("@Value2", DateTime.Now);
    command2.ExecuteNonQuery();
}

Upvotes: 1

Related Questions