DatagridView SAVE Funtion
Sir, this is the result when putting break point.

Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 9:10 PM EDT
tell me some question
why you have declared address,ownername all are NVARCHAR
if you are not going to store other type of culture language (other than english) than declare as VARCHAR only
and phonenumer,housenumber numeric
phonenumber, housenumber can't be in decimal in real time, so better declare as SMALINT
if you thing housenumber can contain alphanumeric then declare as VARCHAR
Regards,
Ifitkar
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 8:37 PM EDT
Can you check your database, what data has been inserted for the house?
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 8:47 PM EDT
Both HouseAdd and PersonAdd is worked when I remove fk relationship from tbl_Person( but not worthy) .
Then in tbl_Person houseId is Zero and all other data are inserted successfully.
the database table of tbl_House is:-
Also one thing, no value being added to the column ownerName.
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 8:55 PM EDT
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 9:01 PM EDT
1) SQL SP
ALTER PROCEDURE houseAdd
@houseNumber numeric(18,0),
@Address nvarchar(20),
@phoneNumber numeric(18,0),
@ownerName nvarchar(20),
@Photo image
AS
DECLARE @houseId int
insert into tbl_House( houseNumber, Address, phoneNumber, ownerName, Photo)
values( @houseNumber, @Address, @phoneNumber,@ownerName,@Photo)
SET @houseId = @@identity
SELECT @houseId as houseId
2)SP class
public int HouseAdd(houseInfo infoHouse)
{
int houseId;
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("houseAdd", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@houseNumber", SqlDbType.Decimal).Value = infoHouse.houseNumber;
sqlCmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = infoHouse.Address;
sqlCmd.Parameters.Add("@phoneNumber", SqlDbType.Decimal).Value = infoHouse.phoneNumber;
sqlCmd.Parameters.Add("@ownerName", SqlDbType.NVarChar).Value = infoHouse.ownerName;
sqlCmd.Parameters.Add("@Photo", SqlDbType.Image).Value = infoHouse.Photo;
houseId = Convert.ToInt32(sqlCmd.ExecuteScalar());
if (houseId > 0)
{
Common.MessageAdd();
}
}
catch (Exception)
{
throw;
}
finally
{
sqlCon.Close();
}
return houseId;
}
3) btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
try
{
personInfo infoPerson = new personInfo();
houseInfo infoHouse = new houseInfo();
personSP SpOwner = new personSP();
houseSP spHouse = new houseSP();
infoHouse.houseNumber = decimal.Parse(txtHouseNumber.Text);
infoHouse.Address = txtAddress.Text;
infoHouse.ownerName = txtowner.Text;
infoHouse.phoneNumber = decimal.Parse(txtFonNo.Text);
infoHouse.Photo = FamilyPhoto;
decimal houseId = spHouse.HouseAdd(infoHouse);
if (dgvFamilyReg.RowCount > 0)
{
for (int i = 0; i <= dgvFamilyReg.Rows.Count - 1; i++)
{
string col1 = dgvFamilyReg.Rows[i].Cells["txpersonName"].Value.ToString();
string col2 = dgvFamilyReg.Rows[i].Cells["txAge"].Value.ToString();
string col3 = dgvFamilyReg.Rows[i].Cells["cmbSex"].Value.ToString();
string col4 = dgvFamilyReg.Rows[i].Cells["cmbRelation"].Value.ToString();
string col5 = dgvFamilyReg.Rows[i].Cells["txOccupation"].Value.ToString();
infoPerson.houseId = houseId;
infoPerson.personName = col1;
infoPerson.Age = decimal.Parse(col2);
infoPerson.Sex = col3;
infoPerson.Relation = col4;
infoPerson.Occupation = col5;
SpOwner.PersonAdd(infoPerson);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Posted Jul 31, 2013, 9:11 PM EDT
Please post sp for person add, and sql profiler trace values also.
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 9:19 PM EDT
could you give me the code to (Use sql profiler to hook what values sent during person add, that will tell you sp parameters and its values. )
Posted Jul 31, 2013, 9:24 PM EDT
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 9:40 PM EDT
public void PersonAdd(personInfo InfoPerson)
{
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("personAdd", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@personName", InfoPerson.personName);
sqlCmd.Parameters.AddWithValue("@Age", InfoPerson.Age);
sqlCmd.Parameters.AddWithValue("@Sex", InfoPerson.Sex);
sqlCmd.Parameters.AddWithValue("@Relation", InfoPerson.Relation);
sqlCmd.Parameters.AddWithValue("@Occupation", InfoPerson.Occupation);
int incount = sqlCmd.ExecuteNonQuery();
if (incount > 0)
{
Common.MessageAdd();
}
}
catch (Exception)
{
throw;
}
finally
{
sqlCon.Close();
}
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 9:43 PM EDT
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 10:00 PM EDT
I know that houseId is not passing to the tbl_Person in the PersonAdd function.
how can it possible?
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 10:05 PM EDT
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 10:16 PM EDT
again I getting the error i posted recently.
Can I modify the PersonAdd(Sql SP, SP class ) same as HouseAdd to get output?
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 10:19 PM EDT
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 10:25 PM EDT
1) houseInfo
class houseInfo
{
public decimal houseId
{
get;
set;
}
public int houseNumber
{
get;
set;
}
public string Address
{
get;
set;
}
public int phoneNumber
{
get;
set;
}
public string ownerName
{
get;
set;
}
public byte[] Photo
{
get;
set;
}
2) personInfo
class personInfo
{
public decimal houseId
{
get;
set;
}
public string personName
{
get;
set;
}
public int Age
{
get;
set;
}
public string Sex
{
get;
set;
}
public string Relation
{
get;
set;
}
public string Occupation
{
get;
set;
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 10:32 PM EDT
Regards,
Iftikar
Bineesh ViswanathRank #1,680 · 907 reputationPosted Jul 31, 2013, 10:48 PM EDT
Now finally I get the result.
Thank you sir for having guided me about two days to get the output. here the modified code.
1) SQL Sp
ALTER PROCEDURE personAdd
@personName nvarchar(20),
@Age numeric(18,0),
@Sex nvarchar(20),
@Relation nvarchar(20),
@Occupation nvarchar(20),
@houseId numeric(18,0)
AS
insert into tbl_Person(houseId,personName, Age, Sex, Relation, Occupation)
values(@houseId,@personName, @Age, @Sex, @Relation, @Occupation)
SELECT @houseId as houseId
2) Sp class(pesonSP)
public void PersonAdd(personInfo InfoPerson)
{
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("personAdd", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@personName", InfoPerson.personName);
sqlCmd.Parameters.AddWithValue("@Age", InfoPerson.Age);
sqlCmd.Parameters.AddWithValue("@Sex", InfoPerson.Sex);
sqlCmd.Parameters.AddWithValue("@Relation", InfoPerson.Relation);
sqlCmd.Parameters.AddWithValue("@Occupation", InfoPerson.Occupation);
sqlCmd.Parameters.AddWithValue("@houseId", SqlDbType.Decimal).Value = InfoPerson.houseId;
int incount = sqlCmd.ExecuteNonQuery();
if (incount > 0)
{
Common.MessageAdd();
}
else
{
MessageBox.Show("Already Exists");
}
}
catch (Exception)
{
throw;
}
finally
{
sqlCon.Close();
}
}
Thank You.
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 31, 2013, 11:11 PM EDT
Regards,
Iftikar