PDA

View Full Version : c# Save button SQL connection fail



jonatas121
04-27-2016, 10:02 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
[Only registered and activated users can see links]
SqlCommand command = new SqlCommand();
SqlDataReader dataRead;

private void eMPRESASBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.eMPRESASBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.cONTATOS);

}

private void Form3_Load(object sender, EventArgs e)
{
command.Connection = ABC;
this.eMPRESASTableAdapter.Fill(this.cONTATOS.EMPRE SAS);



}

private void tELEFONELabel_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
ABC.Open();
command.CommandText="insert into EMPRESAS(NOME EMPRESA,TELEFONE,ENDERE�O,BAIRRO,CONTATOS) Values('"+nOME_EMPRESATextBox.Text+"','"+tELEFONETextBox.Text+"','"+eNDERE�OTextBox.Text+"','"+bAIRROTextBox.Text+"','"+cONTATOSTextBox.Text+"')";
command.ExecuteNonQuery();
ABC.Close();
MessageBox.Show("EMPRESA CADASTRADA COM SUCESSO...!");
nOME_EMPRESATextBox.Clear();
tELEFONETextBox.Clear();
eNDERE�OTextBox.Clear();
bAIRROTextBox.Clear();
cONTATOSTextBox.Clear();

}

private void eMPRESASDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void bAIRROLabel_Click(object sender, EventArgs e)
{

}

private void eMPRESASBindingNavigator_RefreshItems(object sender, EventArgs e)
{

}
}
}



Appears

Network error or specific to the instance when connecting 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 )
and

Warning 1 The field 'WindowsFormsApplication1.Form1.dataRead' is never used C:\Users\Somline\Documents\Visual Studio 2008\Projects\PROGRAMA\WindowsFormsApplication1\Fo rm1.cs 21 23 WindowsFormsApplication1
20586

database string : Data Source=C:\Users\Somline\Documents\Visual Studio 2008\Projects\PROGRAMA\WindowsFormsApplication1\Da tabase1.sdf;Persist Security Info=True

A PROGRAM A LITLE SO LIKE THIS
[Only registered and activated users can see links]
for run only in my PC thanks

j03
04-27-2016, 11:05 AM
Based on the error, your app is not connecting to the database.

Also, you can ignore warnings. And please be more clear?

You also are using VS 2008... There's 2010, 2013 and 2015... You should consider upgrading.



Sent from my iPhone using Tapatalk

jonatas121
04-27-2016, 11:53 AM
I want save a textbox to database, so i use sql connection, but i don't know how to conect with localhost server

private void button1_Click(object sender, EventArgs e)
{
ABC.Open();
appears

Network error or specific to the instance when connecting 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 )