Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert a record into database and display into gridview
but i am getting error
C#
protected void btnsubmit_Click(object sender, EventArgs e)
       {
           cmp c = new cmp();
           c.EmpName = txtempname.ToString();
           c.jobid = Convert.ToInt32(txtjobid.Text);
           c.managerid = Convert.ToInt32(txtmanagerid.Text);
           c.salary = Convert.ToInt32(txtsalary.Text);
           c.hiredate = Convert.ToDateTime(txthiredate.Text);
           Employee a = new Employee();
           a.insertEmployeeData(c);
       }

in DAl
C#
public void insertEmployeeData(cmp c)
      {
          CompanyDataContext db = new CompanyDataContext();
          var list = db.Employees.Where(p => p.empid == c.Employeeid).FirstOrDefault();
          list.empname = c.EmpName;
          list.jobid = c.jobid;
          list.hiredate = c.hiredate;
          list.salary = c.salary;
          list.managerid = c.managerid;
          db.Employees.InsertOnSubmit(list);
          db.SubmitChanges();
      }

Object reference not set to an instance of an object.
C#
list.empname = c.EmpName;

here i am getting error
plz help
me thanq.
Posted
Updated 5-Sep-11 4:35am
v2

var list = db.Employees.Where(p => p.empid == c.Employeeid).FirstOrDefault();


This is your problem .. i believe you are trying to read your record before inserting it.

try to pass the 'cmp' instead of the 'list' like this: (if cmp is not the right object type then use Employee)

public void insertEmployeeData(cmp c)
       {
           CompanyDataContext db = new CompanyDataContext();
           db.Employees.InsertOnSubmit(c);
           db.SubmitChanges();
       }


set the empid for the 'cmp'(or Employee) object if it is not set automatically.
 
Share this answer
 
v2
try this code inserting data in to databse and u have to correct as per your aspects.

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand cmd;
            //SqlDataReader dr;


            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
            con = new SqlConnection(constr);


            //con.Open();
            cmd = new SqlCommand("insert into Reg values( @Username,@Password)", con);

            cmd.Parameters.Add("@Username", SqlDbType.VarChar, 20);
            cmd.Parameters["@Username"].Value = TextBox1.Text;

            cmd.Parameters.Add("@Password", SqlDbType.VarChar, 20);
            cmd.Parameters["@Password"].Value = TextBox2.Text;
            int add = 0;
            try
            {
                con.Open();
                add = cmd.ExecuteNonQuery();
                Label5.Visible = true;
                Label5.Text = "Record Addedd";
                Response.Redirect("yourcurrentpage.aspx");
            }
            catch (Exception err)
            {
                Label5.Text = "Record Already existed";
                Label5.Text += err.Message;
            }

            con.Close();
 
Share this answer
 
v2
Have you checked that your LINQ query actually returns a row. Since you're using FirstOrDefault, if no records match the Employeeid condition a null reference should be returned.

If I'm reading this correctly the root cause is that in btnsubmit_Click you don't set the c.Employeeid at all. This would explain why the query finds no records.

Since you're inserting a record could you simply create a new employee instance and fill it's properties and pass it on.
 
Share this answer
 

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