none
My code show error when i insert data from C# to sql server RRS feed

  • Question

  • I want to insert data from windows application form of visual studio2012 to sql server 2012.But my code show some error.

    why this error ?

    My code is below:

    App.Config:code

      

    <configuration>

        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <ConnectionString>
        <add name="ConString" ConnectionString="Data Source=MALAYROY-PC;Initial Catalog=person; Persist Sequrity Info=True"/>
             ProviderName="System.Data.SqlClient"" />
      </ConnectionString>
    </configuration>

    Form button code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Configuration.Assemblies;



    namespace practice
    {
        public partial class info : Form
        {
            public info()
            {
                InitializeComponent();
            }

            private void btn_save_Click(object sender, EventArgs e)
            {
                string constr = ConfigurationManager.Connection;
                string ["ConString"]. ConnectionString;
                SqlConnection Con =new SqlConnection(constr);
                Con.Open();
                SqlCommand cmd =new SqlCommand();
                cmd.Connection=Con;
                cmd.CommandType=CommandType.Text;
                cmd.CommandText="insert into info values('txt_name.Text+','+txt_mail.Text') ";
                int i=cmd.ExecuteNonQuery();
                if(i>0)
                {
                    MessageBox.Show("Data save successfully");
                    
                }
                else
                {
                    MessageBox.Show("failed");
                }


               
            }

           }
        }

    Tuesday, December 3, 2013 10:13 AM

Answers

  • Your code is not valid C# syntax.

    ConfigurationManager.Connection is not a member of Configuration Manager.

    string ["ConString"] is not valid C# syntax at all.

    You most likely mean

    var constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    

    For that code to compile you need to add a using statement for System.Configuration up with the other using statements.  You also need to ensure that System.Configuration is added as a reference to your project.

    using System.Configuration;

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Malaya Roy Wednesday, December 4, 2013 10:04 AM
    Tuesday, December 3, 2013 6:12 PM
    Moderator

All replies

  • Can you provide details what exception you get?
    Tuesday, December 3, 2013 2:39 PM
  • Your code is not valid C# syntax.

    ConfigurationManager.Connection is not a member of Configuration Manager.

    string ["ConString"] is not valid C# syntax at all.

    You most likely mean

    var constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    

    For that code to compile you need to add a using statement for System.Configuration up with the other using statements.  You also need to ensure that System.Configuration is added as a reference to your project.

    using System.Configuration;

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Malaya Roy Wednesday, December 4, 2013 10:04 AM
    Tuesday, December 3, 2013 6:12 PM
    Moderator
  • Thanks for solution me.

     I face another error when edit my code by this solution. I added using statement System.Configuration with other using statement. But i face new error is -

     "Error 1 The name 'ConfigurationManager' does not exist in the current context ."

    why this error show? Please reply me.

    Wednesday, December 4, 2013 10:03 AM
  • Thanks for solution me.

     I face another error when edit my code by this solution. I added using statement System.Configuration with other using statement. But i face new error is -

     "Error 1 The name 'ConfigurationManager' does not exist in the current context ."

    why this error show? Please reply me.

    Wednesday, December 4, 2013 10:08 AM
  • You need to also add a reference for System.Configuration

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads

    Wednesday, December 4, 2013 10:16 AM