Click here to Skip to main content
12,248,681 members (46,090 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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>
Posted 5-Aug-12 3:50am
Edited 5-Aug-12 3: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
   






this is my connection string
Wes Aday 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.
Wes Aday 5-Aug-12 9:17am
   
The error message tells you exactly what the problem is. What is your question?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

static string constring = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;


I think You used "C" instead of "c" in cnstr.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

where do you open the conection in the sqlcommand?


cmd.ConnectionString=constring;

or
even you can use

SqlCommand cmd = new SqlCommand("in_mem",con);
  Permalink  
v2
Comments
Wes Aday 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);
Wes Aday 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?

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


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100