Dear developers...

I have problem with code... I cant understand where is mistake... here is my code...

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.Sql;
using System.Data.SqlClient;


namespace Phon_Imenik
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con = new SqlConnection("data source = .\\sqlexpress;integrated security = true;Initial Catalog = Tel.Imenik");
            

                
                SqlDataAdapter da = new SqlDataAdapter();

                da.InsertCommand = new SqlCommand("Insert into Osoba values(@Osoba ID,@Ime, @Prezim,@Adresa,@Telefon,@Mob1,@Mob2,@Mob3) ");

                da.InsertCommand.Parameters.Add("@OsobaID", SqlDbType.Int).Value = Convert.ToString(textBox1.Text);
                da.InsertCommand.Parameters.Add("@Ime", System.Data.SqlDbType.NVarChar, 50).Value = textBox2.Text;
                da.InsertCommand.Parameters.Add("@Prezim", System.Data.SqlDbType.NVarChar, 50).Value = textBox3.Text;
                da.InsertCommand.Parameters.Add("@Adresa", System.Data.SqlDbType.NVarChar, 50).Value = textBox4.Text;
                da.InsertCommand.Parameters.Add("@Telefon", System.Data.SqlDbType.NVarChar, 50).Value = textBox5.Text;
                da.InsertCommand.Parameters.Add("@Mob1", System.Data.SqlDbType.NVarChar, 50).Value = textBox6.Text;
                da.InsertCommand.Parameters.Add("@Mob2", System.Data.SqlDbType.NVarChar, 50).Value = textBox7.Text;
                da.InsertCommand.Parameters.Add("@Mob3", System.Data.SqlDbType.NVarChar, 50).Value = textBox8.Text;
           
            
                con.Open();
                da.InsertCommand.ExecuteNonQuery();       
                con.Close();

when I run this code i get a error in line da.InsertCommand.ExecuteNonQuery with message
ExecuteNonQuery: Connection property has not been initialized.

Any one who can hellp me to understand a mistake....

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Please look carefully at the constructor for SqlCommand. Use intellisense to help you.

Edited 6 Years Ago by Ketsuekiame: n/a

Please use code tagging

you didn't hook da with the conn.

Edited 6 Years Ago by finito: n/a

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Exam_Registeration
{
    public partial class _Default : System.Web.UI.Page
    {
         SqlDataAdapter da = new SqlDataAdapter();
         SqlConnection sqlcon = new SqlConnection(@"Data Source=tavlpt-sol-kemi\sqlexpress;Initial Catalog=Exam;Integrated Security=SSPI");
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
          DataBinder ds = new DataBinder();
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand("INSERT INTO Registeration(name, class)VALUES('"+Textname.Text+","+Textclass.Text+"')");
            sqlcmd.Parameters.AddWithValue("@name", Textname.Text);
            sqlcmd.Parameters.AddWithValue("@class", Textclass.Text);
            lbl.Visible = true;
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }
    }
}

This is the error I get

ExecuteNonQuery: Connection property has not been initialized.

I need help pls

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You create a connection object (SqlConnection) but you don't attach it to the SqlDataAdapter. You need to use this constructor for the SqlDataAdapter.

As Momerath said use

da.InsertCommand = new SqlCommand("Insert into Osoba values(@Osoba ID,@Ime, @Prezim,@Adresa,@Telefon,@Mob1,@Mob2,@Mob3) ",con);

Hope it will helps

Edited 5 Years Ago by bhagawatshinde: n/a

 SqlConnection sc = new SqlConnection("Data Source = Destroyed-pc; Initial Catalog = school; Integrated Security = True;");




        using (SqlCommand sa =sc.CreateCommand())
        {
            sa.CommandType = CommandType.Text;
            sa.CommandText = "INSERT INTO tblMember values(@Title,@firstName,@middleName,@lastName,@DOB,@Email";
            sa.Parameters.AddWithValue("@Title", DropDownListTitle.SelectedItem.ToString());
            sa.Parameters.AddWithValue("@firstName", txtFirstName.Text);
            sa.Parameters.AddWithValue("@middleName", txtMiddleName.Text);
            sa.Parameters.AddWithValue("@lastName", txtLastName.Text);
            sa.Parameters.AddWithValue("@DOB", txtDOB.Text);
            sa.Parameters.AddWithValue("@Email", txtEmail.Text);

                sc.Open();
                sa.ExecuteNonQuery();
                sc.Close();

        }

i get this error when i run the code.

Server Error in '/schoolMgmt' Application.

Incorrect syntax near '@Email'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '@Email'.

Source Error:

Line 36:
Line 37: sc.Open();
Line 38: sa.ExecuteNonQuery();
Line 39: sc.Close();
Line 40:

Source File: c:\Users\destroyed\Documents\Visual Studio 2010\WebSites\schoolMgmt\dataEntry.aspx.cs Line: 38

Stack Trace:

[SqlException (0x80131904): Incorrect syntax near '@Email'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950522
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4856715
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1121
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +200
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
dataEntry.btnStudent_Click(Object sender, EventArgs e) in c:\Users\destroyed\Documents\Visual Studio 2010\WebSites\schoolMgmt\dataEntry.aspx.cs:38
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

You really should have started a new thread rather than post to one that is two years old.

But you forgot the closing ) after @Email

This article has been dead for over six months. Start a new discussion instead.