简体   繁体   中英

Strange behavior: ASP.NET to Sql Server

OK. so Before I ask the question here is the background. I have just passed the development stage of ASP.NET and created a fully functional web application on a LOCAL machine. So I transfer the files to a created IIS Webfolder on the windows server that it is to operate on, to see if it runs like it should. It Does!

So the problem is this. When I type information in, I click Button1 and I just get a postback, which is normal if you take a look at my code. I go to check the Database using Sql Server Management and then I saw it.....NOTHING was there!!

Below is some specifics to help you all out on this problem along with the code below.

Specifics:

  • Windows Server 2003
  • SQL Server 2005
  • .NET Framework 2.0
  • IIS 6

The mark up is as expected with correct textBox names and everything

C# Code:

protected void Button1_Click(object sender, EventArgs e)   {
   SqlConnection connection = new SqlConnection(@"Data Source=myServerName;Initial Catalog=myDatabaseName;Integrated Security=True");  

   SqlCommand cmd = new SqlCommand("insert into [PATIENT]([MRN], [PFNAME], [DOB]) "
                    + " Values(@MRN, @PFNAME, @DOB)", connection);

   cmd.Parameters.AddWithValue("@MRN", int.Parse(MRNTextBox.Text));  
   cmd.Parameters.AddWithValue("@PFNAME", NameTextBox.Text);  
   cmd.Parameters.AddWithValue("@DOB", DOBTextBox.Text);

   connection.Open();  
   cmd.ExecuteNonQuery();  
   cmd.Connection.Close();
}

That's the code. I could not even come close to guessing where the problem might be because I don't receive any error messages. Please let me know if you require more information. I will gladly provide.

PS I am running the website through IIS and not the ASP.NET Development Server (if I didn't already mention that. So if it runs on your "View in Browser" from VS then I am would happy to know")

EDIT

I want to thank all for the help. I followed most of your recommendations and I hope that after I figure out the new problem (getting login error exception, YAY!!) I will be able to use your comments in practice. You will all think I am an idiot (which I feel like right about now), but the reason I could not see the changes was due to not building the C# code. I know some mentioned compiling, but I assumed it was compiling because anytime I made a change to markup it showed up instantaneously in the web. I got used to "View in Browser" so much that I forgot Visual Studio auto compiles the code (I think)

The first thing I would aim for is adding some instrumentation in the code. The easiest way is to add some tracing. This will ensure the events are wiring up correctly and events are properly being fired. If you don't see the button click being fired in the trace, then you have a UI problem and can focus on it. Most common is not setting the @ Page directive correctly, but there are other potential reasons.

The next step to troubleshoot is to profile the server you are hitting, if possible. Hopefully you are not deploying directly to production and you have a staging environment you can play with this in.

My final step would be refactor out the actual data code from your UI layer and create a testable "repository" (whether or not you are using a repository pattern -- reason for quotes). You can then surround the code with tests and make sure the code works. You still need to trace to ensure the library is being called and events are wired correctly, but separating concerns will allow you to better handle issues with the data access code.

Okay, not so final. You need to get the magic strings out of your code and move them to the config, where they belong. Hard coding == bad juju.

Hope this helps!

If you are sure that the event is firing then the most likely cause is your use of Integrated Security. Remember that ASP.NET process is not equivalent to your personal login. Integrated security only works if you are logged on and accessing the database via an account or group (eg Administrators) that has been explicitly given an account in the database . The only doubt here is why you aren't getting an error thrown when you attempt to connect.

Do NOT be tempted to give the IIS_IUSRS account access to the database - that is an invitation to trouble. Instead, use a SQL Server account with an appropriate and secure password in your login string. That way, you don't have to worry about the status of IIS_IUSRS with respect to your database.

As a side note, you will quickly grow very tired of explicitly opening connections, setting up commands, adding parameters, etc. I'd strongly suggest that you either adopt a commercial ORM or, better yet, implement a lightweight Data Access Layer that automates much of the grunt work.

The simplest explanation is that you're just hitting the wrong DB. Otherwise you should see some errors. You're not silently catching exceptions are you?

Your code seems fine. You mentioned in your question that you are not getting any exceptions and you code is also not working. May be you can try this so that you atleast catch any exception if you have in your code which might be silently causing the problem.

Add a Global.asax file to your web project and add the Application_Error event to it. Below are the signatures for the same

protected void Application_Error(object sender, EventArgs e)
{
}

This is one event that will catch any exception in your web project, no matter where that exception occurs in your project. Once it hits the break point, check the sender object for AllErrors collection. The code below will help you

(sender as HttpApplication).Context.AllErrors[0]

In case there is some error in your code, it cannot pass this event. It will definitely be caught here.

All the best

To be blunt, use an ORM like LLBLGen Pro

You don't need to reinvent the wheel when it comes to database access. It will help your code be maintainable and lessens the chance of introducing bugs because of writing your own DB access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM