Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It is my Code. when i run my it the error it and Click the Submit Button this error appear

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: ExecuteNonQuery: Connection property has not been initialized."
---------------------------------------------------------------------------------------
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Data_Entry
{
    public partial class mainform : Form
    {
        SqlConnection sqlcon;
        public SqlConnection open()
        {
            sqlcon = new SqlConnection("Data source='LAPTOP-RSHUE2L7\\SQLEXPRESS';initial catalog ='Data Entry'; integrated security ='SSPI';");
            sqlcon.Open();
            return sqlcon;
        }
        public mainform()
        {
            InitializeComponent();
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Buttonsubmitt_Click(object sender, EventArgs e)
        {
            ep.ClearErrors();
            string srno, name, fthername, gendr, cnic, mobile, dob, adrs, vc, thsl, dstrct, ocptn;
            srno = textEditserialno.Text.Trim();
            name = textEditname.Text.Trim();
            fthername = textEditfathername.Text.Trim();
            gendr = comboBoxgender.SelectedIndex.ToString();
            cnic = textEditcnic.Text.Trim();
            mobile = textEditmobileno.Text.Trim();
            dob = dateEditdob.EditValue.ToString();
            adrs = textEditaddress.Text.Trim();
            vc = textEditvc.Text.Trim();
            thsl = textEdittehsil.Text.Trim();
            dstrct = textEditdistrict.Text.Trim();
            ocptn = textEditoccupation.Text.Trim();
            string q = string.Format("Insert into entry(serialno,name, fathername,gender,cnic,mobile,birthdate,address,vc,tehsil,district,occupation) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", srno, name, fthername, gendr, cnic, mobile, dob, adrs, vc, thsl, dstrct, ocptn);
            SqlCommand sc = new SqlCommand(q);
            sc.ExecuteNonQuery();


        }
    }
}


What I have tried:

i tried so many time but i can't find any solution.
Posted
Updated 15-Apr-20 12:39pm
v3

You need to tell your SQLCommand what your sql connection is.
in this case
SqlCommand sc = new SqlCommand(q,sqlcon);


currently your code just tells the command what SQL string to execute without telling it what sql connection to use. Make sure your connection is setup and open before executing your command. (I say that because I didn't notice where you are calling the method that creates and opens your SQL connection even though the code is there).

So somewhere in your form load, or on the button press you need to call your "open()" method to open that sql connection. Then I would recommend closing it and cleaning up after yourself when you are done with the connection and sql command. I know .NET does garbage collection, but cleaning up after yourself is just good practice in my opinion.
 
Share this answer
 
v2
C#
string q = string.Format("Insert into entry(serialno,name, fathername,gender,cnic,mobile,birthdate,address,vc,tehsil,district,occupation) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", srno, name, fthername, gendr, cnic, mobile, dob, adrs, vc, thsl, dstrct, ocptn);

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
AttaUrRahman 17-Apr-20 8:40am    
Thank You Sir.
See this code. is this correct or not. i mean Query.

private void button_save_Click(object sender, EventArgs e)
{
DateTime date = DateTime.Parse (datee.ToString());
string starttime = tb_starttime.Text.Trim();
string endtime = tb_endtime.Text.Trim();
string patienttype = cb_patienttype.SelectedItem.ToString();
string patientfee = tb_patientfee.Text.Trim();
string patientname = tb_patientname.Text.Trim();
string phonenumber = tb_phonenumber.Text.Trim();
string appointmenttype = cb_apointmenttype.SelectedItem.ToString();
string insert = string.Format("insert into doctor values('{0}','{1}','{2}','{3}',{4},'{5}',{6},'{7}')" ,date, starttime, endtime,patienttype,patientfee,patientname,phonenumber,appointmenttype );
SqlCommand scomm = new SqlCommand(insert, dac.open());
scomm.ExecuteNonQuery();

}

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