Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 air
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static string constring = ConfigurationManager.ConnectionStrings["Cnstr"].ConnectionString;
        static SqlConnection con = new SqlConnection(constring);

        private void button1_Click(object sender, EventArgs e)
        {

            SqlCommand cmd = new SqlCommand("in_mem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MemNo", txtmbm.Text);
            cmd.Parameters.AddWithValue("@name", txtnomname.Text);
            cmd.Parameters.AddWithValue("@deptno",txtdept.Text);
            cmd.Parameters.AddWithValue("@empno",txtemp.Text);
            cmd.Parameters.AddWithValue("@d_o_m", txtdom.Text);
            cmd.Parameters.AddWithValue("@d_o_b", txtdob.Text);
            cmd.Parameters.AddWithValue("@d_o_r", txtdor.Text);
            cmd.Parameters.AddWithValue("@nominee_name",txtnomname.Text);
            cmd.Parameters.AddWithValue("@relationship", txtrlt.Text);
            cmd.Parameters.AddWithValue("@address",txtaddr.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
and my connection string

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings >
    <add name="cnstr" providerName="System.Data.SqlClient"
              connectionString="Data Source=localhost;Initial Catalog=airindia_db; Integrated Security=true" />
  </connectionStrings>
</configuration>
Posted
Updated 5-Aug-12 2:56am
v3
Comments
Kenneth Haugland 5-Aug-12 8:54am    
Seems to be something strange with your connection string.
amperayani 5-Aug-12 8:55am    
<configuration>
<connectionstrings>
<add name="cnstr" providername="System.Data.SqlClient"
="" connectionstring="Data Source=localhost;Initial Catalog=airindia_db; Integrated Security=true">


this is my connection string
[no name] 5-Aug-12 9:18am    
A blank line is your connection string?
Richard MacCutchan 5-Aug-12 9:10am    
A couple of minutes with your debugger should reveal exactly what is missing or incorrect in your connection string.
[no name] 5-Aug-12 9:17am    
The error message tells you exactly what the problem is. What is your question?

where do you open the conection in the sqlcommand?


cmd.ConnectionString=constring;

or
even you can use

C#
SqlCommand cmd = new SqlCommand("in_mem",con);
 
Share this answer
 
v2
Comments
[no name] 6-Aug-12 14:11pm    
Since commands do not have a connection string property, how do you expect this to work?
invalidtruck 13-Dec-12 13:12pm    
Is not a connection String property, in the constructor you can pass a SqlConnection like parameter
ex:
var cmd = new SqlCommand();
cmd.Connection= con;
cmd.CommandText = "Query String";

is equal :
SQLConnection oSQLConnection= new SQLConnection("Database string");
var cmd = new SqlCommand("Query String", oSQLConnection);
[no name] 13-Dec-12 13:20pm    
Yes and what exactly does that have to do with your answer that clearly advocates setting the connection string in the command object (cmd.ConnectionString=constring;
) when SqlCommands do not have a connection string property?
static string constring = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;


I think You used "C" instead of "c" in cnstr.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900