Solved

Some fields are not conecting with the database in an aspx file

Posted on 2009-05-11
6
165 Views
Last Modified: 2012-05-06
Please take a look at this page: http://dijitalrealm.com/Default.aspx. If you fill in all the fields and hit submit, only the top 4make it to the database. You can also see that the 3 fields on the bottom don't clear even though the code in the aspx.cs file indicates that they should.

Something is wrong in one or both of the files. I've scrutinized the two files half a dozen times and can't figure it out. The 3 bottom fields (city, state, zip) are exactly as they appear in the database.
This is just a dummy development database, so feel free to enter data in all the fields, submit and then reload the page to see what I mean.

I've attached the code for the aspx and aspx.cs files. I'm hoping you can see something simple and obvious that I just don't see. I've set this up in Dreamweaver if that makes a difference.

Thanks!
John
DEFAULT.ASPX:
 

<form id="form1" runat="server">

    <table>

        <tr>

            <td>First Name:</td>

            <td><asp:TextBox ID="TextBoxFirstName" runat="server" /></td>

        </tr>

        <tr>

            <td>Last Name:</td>

            <td><asp:TextBox ID="TextBoxLastName" runat="server" /></td>

        </tr>

        <tr>

            <td>Email:</td>

            <td><asp:TextBox ID="TextBoxEmailAddress" runat="server" /></td>

        </tr>

        <tr>

            <td>Address:</td>

            <td><asp:TextBox ID="TextBoxAddress" runat="server" /></td>

        </tr>

                <tr>

            <td>City:</td>

            <td><asp:TextBox ID="TextBoxCity" runat="server" /></td>

        </tr>

        <tr>

            <td>State:</td>

            <td><asp:TextBox ID="TextBoxState" runat="server" /></td>

        </tr>

        <tr>

            <td>Zip:</td>

            <td><asp:TextBox ID="TextBoxZip" runat="server" /></td>

        </tr>

        <tr>

            <td>&nbsp;</td>

            <td style="text-align:center"><asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /></td>

        </tr>

    </table>

    <div>

        <asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"></asp:Label>

    </div>
 

</form>
 
 

DEFAULT.ASPX.CS
 

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;
 

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

{
 

	

    protected void Button1_Click(object sender, EventArgs e)

    {

        string connStr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(connStr))

        {

            using (SqlCommand cmd = new SqlCommand("Insert INTO members (first, last, email, address, city, state, zip) VALUES (@FirstName1, @LastName, @EmailAddress, @Address, @City, @State, @Zip)", conn))

            {

                cmd.Parameters.AddWithValue("@FirstName1", this.TextBoxFirstName.Text);

                cmd.Parameters.AddWithValue("@LastName", this.TextBoxLastName.Text);

                cmd.Parameters.AddWithValue("@EmailAddress", this.TextBoxEmailAddress.Text);

                cmd.Parameters.AddWithValue("@Address", this.TextBoxAddress.Text);

				cmd.Parameters.AddWithValue("@City", this.TextBoxCity.Text);

                cmd.Parameters.AddWithValue("@State", this.TextBoxState.Text);

                cmd.Parameters.AddWithValue("@Zip", this.TextBoxZip.Text);
 

                try

                {

                    conn.Open();

                    cmd.ExecuteNonQuery();

					this.TextBoxFirstName.Text = "";

    	            this.TextBoxLastName.Text = "";

	                this.TextBoxEmailAddress.Text = "";

   					this.TextBoxAddress.Text = "";

					this.TextBoxCity.Text = "";

	                this.TextBoxState.Text = "";

   					this.TextBoxZip.Text = "";

                }

                catch (Exception exc)

                {

                    string error = exc.Message;

                    this.ErrorLabel.Text = error;

                }

                finally

                {

                    conn.Close();

                }

            }

        }

    }

}

Open in new window

0
Comment
Question by:gabrielPennyback
6 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 165 total points
ID: 24361084
I have tried to add th record but I am not able to add anything on the same form
I am not seeing anything wrong in the Code
Can you share what you have writeen on Page_Load?
I think create a new page and try it again may help Or just Delete 3 bottom fields (city, state, zip)  and add with differnt id's
0
 
LVL 2

Assisted Solution

by:j4m35bond
j4m35bond earned 165 total points
ID: 24361111
Hi, please put the codes below under page.load and try to run it.
I suspect that the button click event was not triggered.

this.TextBoxFirstName.Text = "";
this.TextBoxLastName.Text = "";
this.TextBoxEmailAddress.Text = "";
this.TextBoxAddress.Text = "";
this.TextBoxCity.Text = "";
this.TextBoxState.Text = "";
this.TextBoxZip.Text = "";
0
 
LVL 4

Accepted Solution

by:
y0usuf earned 170 total points
ID: 24361165
Try extending the try catch block. Start try before you start adding parameters to the sqlcommand.I am not sure , but may be the City field is throwing some exception and after the exception the compiler is going to try statement instead of adding the other two fields. Or may be change the order. Add city field at last

 cmd.Parameters.AddWithValue("@FirstName1", this.TextBoxFirstName.Text);
                cmd.Parameters.AddWithValue("@LastName", this.TextBoxLastName.Text);
                cmd.Parameters.AddWithValue("@EmailAddress", this.TextBoxEmailAddress.Text);
                cmd.Parameters.AddWithValue("@Address", this.TextBoxAddress.Text);
                cmd.Parameters.AddWithValue("@State", this.TextBoxState.Text);
                cmd.Parameters.AddWithValue("@Zip", this.TextBoxZip.Text);
                        cmd.Parameters.AddWithValue("@City", this.TextBoxCity.Text);

Try to change the order and see what it does. I am guessing it will not save only the city field now. Let me know what it happens.
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24361724
Thanks for posting. I don't see page-load anywhere in either the .aspx or the .aspx.cs. I assume it would go in the .aspx file. But where? Please insert it in contaext for me.

y0usuf, I tried moving Vity to the bottom but that didn't do any good.

pratima_mcs, regarding "Delete 3 bottom fields (city, state, zip)  and add with differnt id's", I assume you mean in the aspx.cs file, not in the table itself. Do you mean replace them with any id's as long as they're different?

Thanks,
John
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24361773
You know I can't believe it, it turns out that the problem was that my Default.aspx.cs wasn't updating on the server, even though I was loading dependent files in Dreamweaver every time I previewed! I have to start remembering to check for that all the time I guess.

Sorry for the wild goose chase, points to all of you.

Thanks,
John
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 31580398
Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Search by two fields at the same time using c# radio button 5 39
Host asp.net pages 5 25
Data is not showing from images 15 37
Run time Error 4 34
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now