简体   繁体   English

ASP.NET无法将数据输入数据库

[英]ASP.NET Failing to enter data into database

I recently uploaded my data driven ASP.NET application to a asp hosting service called SmarterAsp.com. 我最近将数据驱动的ASP.NET应用程序上载到名为SmarterAsp.com的ASP托管服务。

Everything in the website works fine, all but 1 feature, data can't be entered into the database. 网站上的所有内容均正常运行,除了1个功能外,其他所有数据均无法输入数据库。

However when using a gridview control to see data in the database the data is displayed perfectly and I can even insert, update and delete data through the gridview. 但是,当使用gridview控件查看数据库中的数据时,数据可以完美显示,我什至可以通过gridview插入,更新和删除数据。

I uploaded my database in a separate space on the hosting service and I was provided with a connection string, I replaced all connection strings in my application with the provided connection string. 我将数据库上传到托管服务的单独空间中,并为我提供了连接字符串,我用提供的连接字符串替换了应用程序中的所有连接字符串。

It works fine in my IDE, the problem only came once I uploaded the website to the hosting service. 它在我的IDE中工作正常,仅在将网站上传到托管服务后问题才出现。

Here is my C# code for inserting data into my database: 这是将数据插入数据库的C#代码:

protected void Button1_Click(object sender, EventArgs e)
{
        using (SqlConnection connection = new SqlConnection(@DataSource=SQL5004.Smarterasp.net;Initial Catalog=DB_9DE115_hmkdatabase;User Id=DB_9DE115_hmkdatabase_admin;Password=YOUR_DB_PASSWORD;"))
        {
            SqlCommand cmd = new SqlCommand(@"INSERT INTO Clients(ClientName)VALUES(@ClientName)");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@ClientName", TextBox1.Text.ToString());
            connection.Open();
            cmd.ExecuteNonQuery();
        }
}

And here is the error I get when attempting to insert the data: Server Error in '/' Application. 这是尝试插入数据时遇到的错误:“ /”应用程序中的服务器错误。

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

Description: 描述:

An unhandled exception occurred during the execution of the current web request. 当前Web请求的执行期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: 异常详细信息:

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

My first thought was that it's a problem with the connection string, but the connection string works fine with the rest of the application. 我首先想到的是连接字符串有问题,但是连接字符串在应用程序的其余部分都能正常工作。 I can enter data into my database through the gridview add function but I cannot enter data using the C# codebehind page. 我可以通过gridview add函数将数据输入数据库,但不能使用C#代码隐藏页面输入数据。 As I said it works fine in my IDE, the problem only came once I uploaded the website to the hosting service. 正如我所说的那样,它在我的IDE中运行良好,只有在将网站上传到托管服务后,问题才出现。

Thanks in advance for the help guys. 在此先感谢您的帮助。 You guys are real life heroes! 你们是现实生活中的英雄!

I sorted out the problem. 我解决了问题。 My problem was where I initialized the connection string and where I opened the connection. 我的问题是在哪里初始化连接字符串以及在哪里打开连接。 I initialized the connection string at the top where the page is initialized. 我在页面初始化的顶部初始化了连接字符串。 And then replaced where the connection string was with connection.open(); 然后将连接字符串替换为connection.open();

Thanks for all the help guys! 感谢所有的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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