user3466934
user3466934

Reputation: 1

Throwing Exception When Saving To Database

I'm having problems when saving text to an SQL database, the save() method. This is the error message i'm getting.

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Heres my code, I was wondering what is the issue.Any suggestions on how to fix this? I would be grateful!

public Form1()
{
    InitializeComponent();
}

private void accountDetailsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    this.Validate();
    this.accountDetailsBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.database1DataSet);
}

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'database1DataSet.AccountDetails' table. You can move, or remove it, as needed.
    this.accountDetailsTableAdapter.Fill(this.database1DataSet.AccountDetails);
}

private void nextButton_Click(object sender, EventArgs e)
{
    Amount = decimal.Parse(amount_InvestedTextBox.Text);
    WeekInterestRate = decimal.Parse(WeekIntTextBox.Text);
    TwoWeekInterestRate = decimal.Parse(TWeekIntTextBox.Text);
    MonthInterestRate = decimal.Parse(MonthIntTextBox.Text);
    ThreeMonthInterestRate = decimal.Parse(ThreeMonthIntTextBox.Text);
    Save();
    Change();
}

private void Save()
{
    SqlConnection cn = new SqlConnection(global::WindowsFormsApplication13.Properties.Settings.Default.Database1ConnectionString);
    string sql = "INSERT INTO AccountDetails (Amount Invested) values('" + amount_InvestedTextBox + ")";
    SqlCommand exesql = new SqlCommand(sql, cn);
    cn.Open();
    exesql.ExecuteNonQuery();
    this.accountDetailsTableAdapter.Fill(this.database1DataSet.AccountDetails);
    cn.Close();
}

private void Change()
{
    Form2 f2 = new Form2();
    f2.Show(this);
    Hide();
}

config file

 <configuration>
<configSections>
</configSections>
<connectionStrings>
    <add name="WindowsFormsApplication13.Properties.Settings.Database1ConnectionString"
        connectionString="Data Source=|DataDirectory|\Database1.sdf"
        providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

Upvotes: 0

Views: 1825

Answers (2)

jmcilhinney
jmcilhinney

Reputation: 54457

Why are you creating a SqlConnection at all? You have a typed DataSet so all interaction with the database should be via table adapters. If you want to insert a record into a table then use the table adapter that corresponds to that table.

The immediate cause of the issue is that you are using a SqlConnection, which is only for SQL Server, with a connection string that is for SQL Server CE. Despite the names, SQL Server and SQL Server CE are quite different and there is a separate ADO.NET provider for each. You don't need to worry about that at all though because, as I said, you should be using your table adapters and they incorporate all the connection, data adapter and command objects.

Upvotes: 1

Vikas Rana
Vikas Rana

Reputation: 1999

There is problem with your connection string. Check your server name and Instance name in the connection string. Check here for more details

Upvotes: 0

Related Questions