I have a problem with inserting a TextBox value into a SqlServer table. I wrote that code, but when I run the form it raises a runtime error: “a network-related or instance-specific error occurred while establishing a connection to sql server”. Thanks in advance.
public partial class Add_Client : Form
{
SqlConnection clientConnection;
string connString;
SqlCommand insertCommand;
public Add_Client()
{
InitializeComponent();
connString = "Data Source=.//INSTANCE2;Initial Catalog=Clients;Integrated Security=True";
clientConnection = new SqlConnection();
clientConnection.ConnectionString = connString;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlCommand insertCommand = new SqlCommand();
insertCommand.Connection = clientConnection;
insertCommand.CommandText = "INSERT INTO Client_Info values(@Client_Name,@AutorizationNo,@IssueType,@Status)";
insertCommand.Parameters.Add("@Client_Name",SqlDbType.NVarChar,60).Value=txt_Name.Text;
insertCommand.Parameters.Add("@AutorizationNo", SqlDbType.Int, 60).Value =txt_Auth.Text.ToString();
insertCommand.Parameters.Add("@Issue", SqlDbType.Text, 200).Value =txt_Iss.Text;
insertCommand.Parameters.Add("@Status", SqlDbType.TexT, 15).Value=txt_sta.TexT;
insertCommand.Connection.Open();
insertCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (clientConnection != null)
{
clientConnection.Close();
}
}
}
}
the .// in the host name in your connection string shouldn’t be there. i wouldn’t expect it to work. you should just have the machine name itself there.