datarow error................

Posted by Anand under C# on 2/24/2012 | Points: 10 | Views : 1020 | Status : [Member] | Replies : 6
i am getting an error in btnsave_Click event that object refrence not set to instance of an object i have created an instance still the error prompts
can anyone please suggest possible solution for this




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
MySqlConnection conn;
DataTable dt;
DataSet ds;
MySqlCommandBuilder builder;
MySqlDataAdapter adp;

DataRow row;
public Form1()
{
InitializeComponent();
}

protected void btnshow_Click(object sender, EventArgs e)
{
conn = new MySqlConnection(@"server=localhost;User Id=root;database=employee");
conn.Open();
adp = new MySqlDataAdapter("select * from employee", conn);
builder = new MySqlCommandBuilder(adp);
ds = new DataSet();
adp.Fill (ds, "employee");
dt = ds.Tables[0];
DataRow dr = dt.Rows[0];
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr[3].ToString();
textBox5.Text = dr[4].ToString();
textBox6.Text = dr[5].ToString();
}

protected void btnsave_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dr[4] = textBox5.Text;
dr[5] = textBox6.Text;
dt.Rows.Add(dr);
adp.Update(ds, "employee");
}
}
}

-


Responses

Posted by: Pradeepkumar417 on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Go through this below modified code.

  using System; 

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
MySqlConnection conn;
DataTable dt = new DataTable();
DataSet ds;
MySqlCommandBuilder builder;
MySqlDataAdapter adp;

DataRow row;
public Form1()
{
InitializeComponent();
}

protected void btnshow_Click(object sender, EventArgs e)
{
conn = new MySqlConnection(@"server=localhost;User Id=root;database=employee");
conn.Open();
adp = new MySqlDataAdapter("select * from employee", conn);
builder = new MySqlCommandBuilder(adp);
ds = new DataSet();
adp.Fill (ds, "employee");
dt = ds.Tables[0];
DataRow dr = dt.Rows[0];
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr[3].ToString();
textBox5.Text = dr[4].ToString();
textBox6.Text = dr[5].ToString();
}

protected void btnsave_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dr[4] = textBox5.Text;
dr[5] = textBox6.Text;
dt.Rows.Add(dr);
adp.Update(ds, "employee");
}
}
}


Thanks & Regards,
Software Engineer,
Pradeep Kumar

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
thanku sir but now its prompting with another i modified the code
little more now its telling cannot find appropriate column
protected void btnsave_Click(object sender, EventArgs e)
{

DataRow dr = dt.NewRow();
dr["id"] = textBox1.Text;
dr["firstname"] = textBox2.Text;
dr["Lastname"] = textBox3.Text;
dr["Salary"] = textBox4.Text;
dr["city"] = textBox5.Text;
dr["description"] = textBox6.Text;
dt.Rows.Add(dr);
adp.Update(ds, "employee");
}

-

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Check whether all column names you written correct or not.

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 2/24/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Anand,
main problem you are facing is...
before btnSave_Click event fire you datatable doesn't instanciate properly.
previously it was null, but now though it's not null but still columns is not there..

so i would like to suggest you..first create proper structure as you want in datatable.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hmanjarawala sir
.first create proper structure as you want in datatable.
means


-

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 2/24/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Anand,

plz add DataColumns to your DataTable.

DataTable dt = new DataTable();

// Repeat below statement as many columns you want to add
dt.Columns.Add("<your-column-name>", "<you-column-datatype>")


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response