0

My asp.net is not accessing the .mdf database file.

Local site works but when you attach a server application is down when you click on the button (ADD/MYORDERS) that writes in the database (visit a link). My site is hosted HERE

This is the connection config:

<add name="ConnectionString" 
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30"
     providerName="System.Data.SqlClient" />

This is the code of the button

protected void IB_ADDPred0_Click(object sender, ImageClickEventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    con.Open();

    string insCmd = "Insert into Orders (RoomNo, ProductNo, ProductName, Price) values (@RoomNo, @ProductNo, @ProductName, @Price)";

    SqlCommand addOrder = new SqlCommand(insCmd, con);

    addOrder.Parameters.AddWithValue("@RoomNo", computer_name[0].ToString());
    addOrder.Parameters.AddWithValue("@ProductNo", ProductNo0.Text);
    addOrder.Parameters.AddWithValue("@ProductName", NamePred0.Text);
    addOrder.Parameters.AddWithValue("@Price", PricePred0.Text);

    try
    {
        addOrder.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception er)
    {
        Response.Write("Error. Try again.");
    }
}

Web application works normal when tested locally, but the server is the problems.

This is my web.config:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <sessionState cookieless="true" regenerateExpiredSessionId="true" />
    <customErrors mode="Off"/>
  </system.web>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s Mar 01 '15 at 21:36
  • 1
    `LocalDB` is a **development-only** solution - it's **not** intended to be used in production environments, and it doesn't allow remote connections. You'll need to use **at least** SQL Server **Express** (and *enable remote connections* since those are **OFF** by default for Express) to host your database – marc_s Mar 01 '15 at 21:38
  • _The application crashes_ do you have some more meaningful information than this? For example, instead of writing _Error Try Again_ could you print the exception message? – Steve Mar 01 '15 at 21:38
  • And to complete the observation from @marc_s about AddWithValue. You are passing a string in the value for the Price parameter and the AddWithValue takes the hint and creates a parameter of type nvarchar that is used to set the value for the Price field. Is the Price field a string? – Steve Mar 01 '15 at 21:47
  • 1
    Price is INT Can something be make to not adapt the entire application? – Mila Simonovska Mar 01 '15 at 21:54
  • 1
    This is my web.config ` ` – Mila Simonovska Mar 01 '15 at 21:56
  • Please remove 2 mb image from your demo web site landing page to make it browsable. – abatishchev Mar 01 '15 at 22:24

1 Answers1

0

The problem will solve at the if the make a web-hosting a new database and then Visual Studio just connect to it.

Download it from DDL current base and set of new to save time.

You probably use Windows hosting site. In parallels you can do it if the you create a new or transfer of old.

nAPL
  • 60
  • 5