suman kumar

suman kumar

  • NA
  • 26
  • 23.4k

Object reference not set to an instance of an object.

Aug 9 2013 12:31 AM

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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Configuration;
  11. using System.Data.SqlClient;
  12. namespace WindowsFormsApplication1
  13. {
  14. public partial class Form1 : Form
  15. {
  16. SqlConnection con;
  17. SqlCommand cmd;
  18. SqlDataAdapter sda;
  19. SqlDataReader sdr;
  20. DataSet ds;
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25. private void Form1_Load(object sender, EventArgs e)
  26. {
  27. string st = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;//Object reference not set to an instance of an object.
  28. con = new SqlConnection(st);
  29. fill();
  30. }
  31. public void fill()
  32. {
  33. if (con.State != ConnectionState.Open)
  34. {
  35. con.Open();
  36. }
  37. string qry = "Select *from Table";
  38. cmd = new SqlCommand(qry, con);
  39. sda = new SqlDataAdapter(cmd);
  40. ds = new DataSet();
  41. sda.Fill(ds);
  42. ds.Tables[0].TableName = "abc";
  43. dataGridView1.DataSource = ds.Tables["abc"];
  44. }
  45. private void btn_Save_Click(object sender, EventArgs e)
  46. {
  47. string qry = "INSERT INTO Table(Name,Roll,Grade)Values('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "')";
  48. cmd = new SqlCommand(qry, con);
  49. cmd.ExecuteNonQuery();
  50. fill();
  51. textBox1.Text = "";
  52. textBox2.Text = "";
  53. textBox3.Text = "";
  54. }
  55. private void btn_Delete_Click(object sender, EventArgs e)
  56. {
  57. string qry = "DELETE FROM Table WHERE Name='" + textBox1.Text + "'";
  58. cmd = new SqlCommand(qry, con);
  59. cmd.ExecuteNonQuery();
  60. fill();
  61. MessageBox.Show("File Deleted");
  62. textBox1.Text = "";
  63. textBox2.Text = "";
  64. textBox3.Text = "";
  65. }
  66. private void btn_update_Click(object sender, EventArgs e)
  67. {
  68. string qry = "Update Table set Name = '" + textBox1.Text + "',Grade = '" + textBox3.Text + "' where Roll = '" + textBox2.Text + "'";
  69. cmd = new SqlCommand(qry, con);
  70. cmd.ExecuteNonQuery();
  71. fill();
  72. textBox1.Text = "";
  73. textBox2.Text = "";
  74. textBox3.Text = "";
  75. }
  76. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  77. {
  78. }
  79. }
  80. }

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?


Answers (7)