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){}}}
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?
Posted Sep 26, 2015, 10:33 AM
Sukshith StanleyPosted Sep 26, 2015, 8:15 AM
Private sub bttn_click
Myconn.open()
Dim query as string="select cost from dogs where breed='labrador'"
Dim da as oledbdatareader
Dim codger as new oledbcommand(query, Myconn)
Da=codger.executereader
While da.read
Pet35.text=(Da("cost"))
End while
Myconn.close()
Error is in Myconn.open it's showin NULLREFERNCE EXCEPTION WAS UNHANDLED
Plz help
suman kumarPosted Aug 9, 2013, 4:41 AM
thanks Murali and sunny for ur input,I appreciate it very much :)
Posted Aug 9, 2013, 4:38 AM
suman kumarPosted Aug 9, 2013, 4:36 AM
I want to add one more thing that I installed 2012 ultimate version but in sql server folder Mysqlexpress folder is missing. So can this be the reason for not be able to connect with server?
and to sunny K,I am making windows apps so i dont think I am gonna ned web config,but its was agood Tp though.
Thanks:)
Sunny SharmaPosted Aug 9, 2013, 1:11 AM
Check the connection string inside your web.config file. It lies under Configuration > connectionStrings. Check if it is pointing to the right database and has proper syntax. If you're not sure about the syntax, I recommend you visit "www.connectionstrings.com", here you'll find all the syntax for almost every type of connection strings.
Hope it helps.
Posted Aug 9, 2013, 12:42 AM
The above code is not able to get the connection string information from the app.config file?
Do you have app.config exists in your project? If so, can you please upload app.config file here?