2
Answers

i got a error like this but it's saving in database

Safeer Safeer

Safeer Safeer

11y
944
1
it's saving fine .., but after that one message box showing object reference can not set to an instance of an object








here it is form button save ()
 private void btnSave_Click(object sender, EventArgs e)
  {
  try
  {
  memberinfo infomember = new memberinfo();
  houseinfo infohouse = new houseinfo();
  membersp spmember = new membersp();
  housesp sphouse = new housesp();
  infohouse.houseNo = decimal.Parse(txthouse.Text);
  infohouse.Address = txtaddress.Text;
  infohouse.owner = txtowner.Text;
  infohouse.phone = decimal.Parse(txtphone.Text);

  int houseId = sphouse.houseadd(infohouse);
  if (dataGridView1.RowCount > 0)
  {
  for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
  {
  string col1 = dataGridView1.Rows[i].Cells["txtName"].Value.ToString();
  string col2 = dataGridView1.Rows[i].Cells["txtAge"].Value.ToString();

  string col3 = dataGridView1.Rows[i].Cells["cmbSex"].Value.ToString();

  string col4 = dataGridView1.Rows[i].Cells["cmbRelation"].Value.ToString();

  string col5 = dataGridView1.Rows[i].Cells["txtOccupation"].Value.ToString();


  infomember.houseId = houseId;
  infomember.Name = col1;
  infomember.Age =decimal.Parse( col2);
  infomember.Sex = col3;
  infomember.Relation = col4;
  infomember.Occupation = col5;
  spmember.memberadd(infomember);
  }
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }




and the housesp



 public int houseadd(houseinfo infohouse)
  {
  int houseId;
  try
  {
  if (sqlcon.State == ConnectionState.Closed)
  {
  sqlcon.Open();
  }
  SqlCommand c = new SqlCommand("houseadd", sqlcon);
  c.CommandType = CommandType.StoredProcedure;
  c.Parameters.Add("@houseNo", SqlDbType.Decimal).Value = infohouse.houseNo;
  c.Parameters.Add("@Address", SqlDbType.VarChar).Value = infohouse.Address;
  c.Parameters.Add("@phone", SqlDbType.Decimal).Value = infohouse.phone;
  c.Parameters.Add("@owner", SqlDbType.VarChar).Value = infohouse.owner;
  houseId = Convert.ToInt32(c.ExecuteScalar());
  if (houseId > 0)
  {
  MessageBox.Show("Saved");
  }
  }
  catch (Exception)
  {
  throw;
  }
  finally
  {
  sqlcon.Close();
  }
  return houseId;



and the membersp


public int memberadd(memberinfo infomember)
  {
  int memberId;
  try
  {
  if (sqlcon.State == ConnectionState.Closed)
  {
  sqlcon.Open();
  }
  SqlCommand c = new SqlCommand("memberadd", sqlcon);
  c.CommandType = CommandType.StoredProcedure;
  c.Parameters.Add("@houseId", SqlDbType.Decimal).Value = infomember.houseId;
  c.Parameters.Add("@Name", SqlDbType.VarChar).Value = infomember.Name;
  c.Parameters.Add("@Age", SqlDbType.Decimal).Value = infomember.Age;
  c.Parameters.Add("@Sex", SqlDbType.VarChar).Value = infomember.Sex;
  c.Parameters.Add("@Relation", SqlDbType.VarChar).Value = infomember.Relation;
  c.Parameters.Add("@Occupation", SqlDbType.VarChar).Value = infomember.Occupation;
  memberId = Convert.ToInt32(c.ExecuteScalar());
  if (memberId > 0)
  {
  MessageBox.Show("Saved");
  }
  }
  catch (Exception)
  {
  throw;
  }
  finally
  {
  sqlcon.Close();
  }
  return memberId;
  }
  }


Answers (2)