Edit/Delete button in asp.net c#
<p>I have a form that lists customers pulled from a db.  The form also consists of edit and delete buttons in order to update an existing customer's record.  I am able to pull back all the table data for the customers on the first page.  However, when I click the "Edit" button next to a customer it does not bring back any data but just brings back a blank form.  I am referencing the CustomerID in the "Edit" hyperlink with a NavigateURL link.  I have read and read and am getting confused about how to do this. Can anyone see what I may be doing wrong? Getting very frustrated. Please help.</p>




<p></p>

<pre><code>&lt;div&gt;
    &lt;h2&gt;Customer Listing&lt;/h2&gt;
    &lt;br /&gt;
&lt;/div&gt;

&lt;p style="text-align:center"&gt;
    &lt;asp:Button ID="btnCustomer" class="button" runat="server" Text="Add New Customer" onclick="btnCustomer_Click" /&gt;
&lt;/p&gt;


&lt;asp:ListView ID="lv" runat="server" 
    onselectedindexchanged="lv_SelectedIndexChanged"&gt;
&lt;LayoutTemplate&gt;
  &lt;table width="110%" class="TableListing"&gt;
  &lt;tbody&gt;
    &lt;thead&gt;
    &lt;th width="150"&gt;Customer Name&lt;/th&gt;
      &lt;th width="150"&gt;Email&lt;/th&gt;
      &lt;th width="150"&gt;City&lt;/th&gt;
      &lt;th width="40"&gt;State&lt;/th&gt;
      &lt;th width="110"&gt;Phone&lt;/th&gt;
      &lt;th width="80"&gt;Modify&lt;/th&gt;
    &lt;/thead&gt;
    &lt;tr id="itemPlaceholder" runat="server"&gt;&lt;/tr&gt;
     &lt;/tbody&gt;
    &lt;/table&gt;  

    &lt;asp:DataPager ID="ItemDataPager" runat="server" PageSize="20"&gt;
        &lt;Fields&gt;
            &lt;asp:NumericPagerField ButtonCount="5" /&gt;
        &lt;/Fields&gt;
    &lt;/asp:DataPager&gt;

&lt;/LayoutTemplate&gt;

&lt;ItemTemplate&gt;
    &lt;tr&gt;
     &lt;td&gt;&lt;%# Eval ("LastName") %&gt;, &lt;%# Eval ("FirstName") %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%# Eval ("Email") %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%# Eval ("City") %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%# Eval ("State") %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%# Eval ("Phone") %&gt;&lt;/td&gt;
     &lt;td&gt;
        &lt;asp:HyperLink ID="lnkEdit" runat="server" NavigateUrl='&lt;%# "CustomerEdit.aspx?ID=" + Eval("CustomerID") + Request.QueryString["LastName"] + Eval("LastName") %&gt;' Text="Edit" /&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
&lt;/ItemTemplate&gt;
</code></pre>

<p></p>

<p></p>

<p>I then redirect to the CustomerEdit page:</p>

<pre><code>    protected void Page_Load(object sender, EventArgs e)
    {
        this.Master.HighlightNavItem("Customers");

        int CustomerID = 0;


        //Declare the connection object
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        //Connect to the db
        Conn.Open();

    //Define query
        string sql = "SELECT * FROM Customer where CustomerID=@CustomerID";

    //Declare the Command
    SqlCommand cmd = new SqlCommand(sql, Conn);

    //Add the parameters needed for the SQL query
    //cmd.Parameters.AddWithValue("@LastName", LastName);

    //Declare the DataReader
    SqlDataReader dr = null;

    //Fill the DataReader
    dr = cmd.ExecuteReader();

    //Get the data
    if (dr.Read() == false)
    {
        //No Records
        dr.Close();
        Conn.Close();
        return;
    }

    txtFirstName.Text = dr["FirstName"].ToString();
    txtLastName.Text = dr["LastName"].ToString();

    dr.Close();
    Conn.Close();


    }


    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Customers.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        //Declare the connection object
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        //Connect to the db
        Conn.Open();

        //Define query
        string sql = "INSERT INTO Customer (FirstName, LastName, Email, Password, Address1, Address2, City, State, Zip, Phone, Fax) VALUES (@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
        //sql = "INSERT INTO xSample(Region,RepName,...) VALUES(@Region,@RepName,...)


        //Declare the Command
        SqlCommand cmd = new SqlCommand(sql, Conn);

        //Add the parameters needed for the SQL query
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail1.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword1.Text);
        cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
        cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
        cmd.Parameters.AddWithValue("@City", txtCity.Text);
        cmd.Parameters.AddWithValue("@State", txtState.Text);
        cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
        cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
        cmd.Parameters.AddWithValue("@Fax", txtFax.Text);

        //Execute the query
        int NumRows = 0;
        NumRows = cmd.ExecuteNonQuery();

        Conn.Close();

        lblUpdate.Text = "Updated " + NumRows.ToString() + " record";

    }

}
</code></pre>

<p>}</p>
