Okay,First of all let me introduce myself as newbee to this language.
this is my third day with program,I usually Practice at college where Visual studio 2010 is installed.
I have VS2012 ultimate.
If I sound silly forgive me,but I am really desperate here.

1st Problem,I couldnt create New SQL server database,so I added a newone with name database.mdf.
2nd I ceated a table and so on...now lets come to the code I have written.

Also I forgot to mention that same code works great with VS 2010 at my college PC.
Now I am confused.

error "Object reference not set to an instance of an object." at Form1_Load..I have mention in code too.

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter sda;
        SqlDataReader sdr;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string st = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;//Object reference not set to an instance of an object.
            con = new SqlConnection(st);
            fill();

        }

        public void fill()
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }

            string qry = "Select *from Table";
            cmd = new SqlCommand(qry, con);
            sda = new SqlDataAdapter(cmd);
            ds = new DataSet();
            sda.Fill(ds);
            ds.Tables[0].TableName = "abc";
            dataGridView1.DataSource = ds.Tables["abc"];
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string qry = "INSERT INTO Table(Name,Roll,Grade)Values('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "')";
            cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();
            fill();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {

            string qry = "DELETE FROM Table WHERE Name='" + textBox1.Text + "'";
            cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();
            fill();
            MessageBox.Show("File Deleted");
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        private void btn_update_Click(object sender, EventArgs e)
        {

            string qry = "Update Table set Name = '" + textBox1.Text + "',Grade = '" + textBox3.Text + "' where Roll = '" + textBox2.Text + "'";
            cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();
            fill();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

Edited 3 Years Ago by salesman91: adding

one more thing ,I couldnt create dbo file(as I couldnt create New SQL database),so I created a new .mdf via add new connection.
Does this affect the coding?

Can you post your app.config file contents please. This hints that the connection string isn't in your config file.

here is the apps config.
I am really frustrated here please help:(

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\user\Documents\database.mdf;Integrated Security=True;Connect Timeout=30"></add> 
</connectionStrings>
</configuration>

Edited 3 Years Ago by salesman91: editing

okay nevermind,I just deleted all my APs.config file from system and made a new one.Now it works.
Thank u all for ur support.
I am so glad to have this forum where I could ask all the stupid questions and no0one make fun of me.
cheers:P

This question has already been answered. Start a new discussion instead.