Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
C#
SqlConnection con = new SqlConnection();
            con.Open();
            con.ConnectionString = "Data Source=GHARONDA-15; Initial Catalog=RagestrationDetail; Integrated Security=SSPI";
            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection=con;
            con.Close();
            cmd.CommandType=CommandType.Text;
            cmd.CommandText= "insert into Form1(Name,FatherName,Address,DateOfBirth,JoiningDate,Experience,EducationalQualification)values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Saved");

Error is
The ConnectionString property has not been initialized.
Posted
Updated 18-May-15 22:14pm
v2

There are a lot of problems here!
The problem you have noticed is pretty simple: You are setting the connection string after you try to open the connection.
And please, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead
Try this:
C#
using (SqlConnection con = new SqlConnection(@"Data Source=GHARONDA-15; Initial Catalog=RagestrationDetail; Integrated Security=SSPI"))
    {
    con.Open();    
    using (SqlCommand cmd = new SqlCommand("INSERT INTO Form1 (Name, FatherName, Address, DateOfBirth, JoiningDate, Experience, EducationalQualification) VALUES (@NM, @FN, @AD, @DOB, @JD, @EX, @EQ)", con))
        {
        cmd.Parameters.AddWithValue("@NM", textBox1.Text);
        cmd.Parameters.AddWithValue("@FN", textBox2.Text);
        cmd.Parameters.AddWithValue("@AD", textBox3.Text);
        cmd.Parameters.AddWithValue("@DOB", textBox4.Text);
        cmd.Parameters.AddWithValue("@JD", textBox5.Text);
        cmd.Parameters.AddWithValue("@EX", textBox6.Text);
        cmd.Parameters.AddWithValue("@EQ", textBox7.Text);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Record Saved");
        }
    }
But...You need to validate the dates are "real" dates, or SQL will throw an exception - and I'm hoping you are storing them as DATETIME values, or you will be giving yourself some really nasty problems in future.

And do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox4" is the date of birth today, but when you have to modify it is three weeks time, will you then? Use descriptive names - "tbBirthdate" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbBirthdate" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
v2
Just Define con.ConnectionString property before con.Open()

and one more thing use con.Close() after your cmd.ExecuteNonQuery()

:-)
 
Share this answer
 
v2
you have to Open and Close Connection object

Quote:

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=GHARONDA-15; Initial Catalog=RagestrationDetail; Integrated Security=SSPI";
SqlCommand cmd = new SqlCommand();
cmd.Connection=con;
using(con.open())
{
cmd.CommandType=CommandType.Text;
cmd.CommandText= "insert into Form1(Name,FatherName,Address,DateOfBirth,JoiningDate,Experience,EducationalQualification)values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}
 
Share this answer
 
v2
con.Open();

should be called after connection string is innitlized



SqlConnection con = new SqlConnection();

con.ConnectionString = "Data Source=GHARONDA-15; Initial Catalog=RagestrationDetail; Integrated Security=SSPI";
con.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection=con;
con.Close();
cmd.CommandType=CommandType.Text;
cmd.CommandText= "insert into Form1(Name,FatherName,Address,DateOfBirth,JoiningDate,Experience,EducationalQualification)values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
 
Share this answer
 
v2

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