Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:
System.InvalidOperationException was unhandled by user code
Message="ExecuteNonQuery: Connection property has not been initialized."
Source="System.Data"
StackTrace:
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at _Default.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\Somesh\Desktop\assigment\Default.aspx.cs:line 32
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:


Code:

C#
using System;
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.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["siteConnectionString1"].ConnectionString);
        SqlCommand cm = new SqlCommand("insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)");
        cm.CommandType = CommandType.Text;
                cm.Parameters.AddWithValue("@first", txtFirstName.Text);
                cm.Parameters.AddWithValue("@last", txtSecondName.Text);
                cm.Parameters.AddWithValue("@email", txtFirstName.Text);
                cm.Parameters.AddWithValue("@mobile", txtFirstName.Text);
                cm.Parameters.AddWithValue("@gender", txtFirstName.Text);
                cm.Parameters.AddWithValue("@country", txtFirstName.Text);
                con.Open();
                cm.ExecuteNonQuery();
                con.Close();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {

    }
}


Config file:


add name="siteConnectionString1" connectionstring="Data Source=SOMESH-PC\SQLEXPRESS;Initial Catalog=site;Integrated Security=True" providerName="System.Data.SqlClient"
Posted
Updated 9-Jul-13 14:12pm
v3

How about

C#
con.Open();


between these 2 lines ..

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["siteConnectionString1"].ConnectionString);
SqlCommand cm = new SqlCommand("insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)");


ok, this is skipping error handling, you should be using a try/catch here as well, lets ignore that for the moment
 
Share this answer
 
Comments
Somesh Pursnani 9-Jul-13 19:17pm    
ohk, after many debugging exercise i missed passing CONNECTION argument in "SqlCommand cm = new SqlCommand("insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)", con);"
compare with the one posted. :)
Garth J Lancaster 9-Jul-13 19:18pm    
yup, I did wonder about that, but got distracted before I could check the syntax :-(
ah well - debugging skills are useful
C#
SqlCommand cm = new SqlCommand("insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)",con);
 
Share this answer
 
I think you are missing
cm.connection = Databaseconnection;
 
Share this answer
 
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["siteConnectionString1"].ConnectionString);
con.Open();
        SqlCommand cm = new SqlCommand("insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)",con);


You have missed the con variable in Sql Command Please Check The article for more info
Insert Operations[^]
 
Share this answer
 
C#
cm.Connection=con;
 
Share this answer
 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["siteConnectionString1"].ConnectionString);

con.open();
sqlcommand cmd=con.createcommand();
cmd.commandType=commandtype.Text;
cmd.commandtext ="insert into new_user (first,last,email,mobile,gender,country) values(@first,@last,@email,@mobile,@gender,@country)";
cm.Parameters.AddWithValue("@first", txtFirstName.Text);
cm.Parameters.AddWithValue("@last", txtSecondName.Text);
cm.Parameters.AddWithValue("@email", txtFirstName.Text);
cm.Parameters.AddWithValue("@mobile", txtFirstName.Text);
cm.Parameters.AddWithValue("@gender", txtFirstName.Text);
cm.Parameters.AddWithValue("@country", txtFirstName.Text);
cmd.ExecuteNonQuery();
con.Close();


I hope this work ...
 
Share this answer
 
Even after passing connection string, I am getting the error...?
Can Any One Help me out....???
Please
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Windows;
using System.ComponentModel;
using System.IO;
using System.Runtime;
public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) { }
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
try
{
//Label lblMessage;
SqlConnection con = new SqlConnection();
con.ConnectionString = "data source=opTLP-4\\SQLExpress;User Id=test;Password=test;Initial Catalog=test";
//SqlCommand cmd = new SqlCommand();
try
{
if (txtUserId.Text == "null")
{
con.Open();
SqlCommand cmd = new SqlCommand("AddUpdateEmpData_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter UserId = cmd.Parameters.Add("@UserId", SqlDbType.NVarChar);
UserId.Direction = ParameterDirection.Output;
SqlParameter Title = cmd.Parameters.Add("@Title", SqlDbType.NVarChar);
Title.Value = txtTitle.Text;
SqlParameter FirstName = cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar);
FirstName.Value = txtFirstName.Text;
SqlParameter MiddleName = cmd.Parameters.Add("@MiddleName", SqlDbType.NVarChar);
MiddleName.Value = txtMiddleName.Text;
SqlParameter LastName = cmd.Parameters.Add("@LastName", SqlDbType.NVarChar);
LastName.Value = txtLastName.Text;
SqlParameter DisplayName = cmd.Parameters.Add("@DisplayName", SqlDbType.NVarChar);
DisplayName.Value = txtDisplayName.Text;
SqlParameter Salutation = cmd.Parameters.Add("@Salutation", SqlDbType.NVarChar);
Salutation.Value = txtSalutation.Text;
SqlParameter Gender = cmd.Parameters.Add("@Gender", SqlDbType.NChar);
Gender.Value = ddlGender.Text;
SqlParameter ManagerID = cmd.Parameters.Add("@ManagerID", SqlDbType.NVarChar);
ManagerID.Value = txtManagerID.Text;
SqlParameter Email = cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
Email.Value = txtEmail.Text;
SqlParameter AlternateEmail = cmd.Parameters.Add("@AlternateEmail", SqlDbType.NVarChar);
AlternateEmail.Value = txtAlternateEmail.Text;
SqlParameter Phone = cmd.Parameters.Add("@Phone", SqlDbType.NVarChar);
Phone.Value = txtPhone.Text;
SqlParameter PhoneExt = cmd.Parameters.Add("@PhoneExt", SqlDbType.NVarChar);
PhoneExt.Value = txtPhoneExt.Text;
SqlParameter Phone1 = cmd.Parameters.Add("@Phone1", SqlDbType.NVarChar);
Phone1.Value = txtPhone1.Text;
SqlParameter Phone1Ext = cmd.Parameters.Add("@Phone1Ext", SqlDbType.NVarChar);
Phone1Ext.Value = txtPhone1Ext.Text;
SqlParameter Fax = cmd.Parameters.Add("@Fax", SqlDbType.NVarChar);
Fax.Value = txtFax.Text;
SqlParameter Mobile = cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar);
Mobile.Value = txtMobile.Text;
SqlParameter Pager = cmd.Parameters.Add("@Pager", SqlDbType.NVarChar);
Pager.Value = txtPager.Text;
SqlParameter CreatedOn = cmd.Parameters.Add("@CreatedOn", SqlDbType.DateTime);
CreatedOn.Value = txtCreatedOn.Text;
SqlParameter CreatedBy = cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar);
CreatedBy.Value = txtCreatedBy.Text;
SqlParameter ModifiedOn = cmd.Parameters.Add("@ModifiedOn", SqlDbType.DateTime);
ModifiedOn.Value = txtModifiedOn.Text;
SqlParameter ModifiedBy = cmd.Parameters.Add("@ModifiedBy", SqlDbType.NVarChar);
ModifiedBy.Value = txtModifiedBy.Text;
// con.Open();
cmd.ExecuteNonQuery();
con.Close();
//ClearAll();
lbloutput.Text = "Record inserted successfully. ID = " + UserId.Value.ToString();

}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
con.Close();
}
}
catch(Exception ex)
{ Response.Write(ex.Message.ToString()); }
Response.Redirect("Default.aspx");
}
}
 
Share this answer
 
v2
Comments
Richard Deeming 20-Oct-15 10:22am    
Do not post your questions as solutions to other questions.

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



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