Click here to Skip to main content
11,479,412 members (37,281 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
This is my code;

private void btnsave_Click(object sender, EventArgs e)
{
if (txtOffinceNo.Text.Trim() == string.Empty)
{
MessageBox.Show("OffinceNumber cannot be left blank.!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (txtRegNo.Text.Trim() == string.Empty)
{
MessageBox.Show("RegNo cannot be left blank.!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

SqlDataAdapter da = new SqlDataAdapter("Select * From TBL_TXN_XML_DATA ", ConfigurationSettings.AppSettings["TrafficDB"]);
DataSet ds = new DataSet("MastData");
da.FillSchema(ds, SchemaType.Source, "TBL_TXN_XML_DATA");
da.Fill(ds, "TTBL_TXN_XML_DATA");
DataTable dt;
dt = ds.Tables["TBL_TXN_XML_DATA"];

DataRow drCurrent;
try
{
if (Convert.ToInt32(txtOffinceNo.Tag.ToString()) == 0)
txtOffinceNo.Tag = 0;
}
catch (System.NullReferenceException ex)
{

txtOffinceNo.Tag = 0;
}


drCurrent = dt.Rows.Find(Convert.ToInt32(txtOffinceNo.Tag.ToString()));
if (drCurrent != null)
{


drCurrent.BeginEdit();
drCurrent["OffinceNo"] = txtFileNo.Text.Trim();
drCurrent["RegNo"] = txtRegNo.Text.Trim();
drCurrent["PlateType"] = txtPlateType.Text.Trim();
drCurrent["Platecolor"] = txtPlateColorName.Text.Trim();
drCurrent["Regsource"] = txtRegsource.Text.Trim();
drCurrent["vechclass"] = txtVehClass.Text.Trim();
drCurrent.EndEdit();
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da);
da.Update(ds, "TTBL_TXN_XML_DATA");
MessageBox.Show("Record updated successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);


}
else
{
drCurrent = dt.NewRow();
drCurrent["OffinceNo"] = txtFileNo.Text.Trim();
drCurrent["RegisNo"] = txtRegNo.Text.Trim();
drCurrent["PlateType"] = txtPlateType.Text.Trim();
drCurrent["Platecolor"] = txtPlateColorName.Text.Trim();
drCurrent["Regsource"] = txtRegsource.Text.Trim();
drCurrent["Vehclass"] = txtVehClass.Text.Trim();
dt.Rows.Add(drCurrent);
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da);
da.Update(ds, "TBL_TXN_MAIN");
MessageBox.Show("Record saved successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
if (txtPlateType.Text.Trim() == "999")
{
PanelGCC.Visible = true;
txtCountryCode.Focus();
drCurrent["country"] = txtCountryCode.Text.Trim();
drCurrent["Plate_issued"] = txtPlateIssuedName.Text.Trim();
drCurrent["Platecode"] = txtPlateType.Text.Trim();
drCurrent["Division"] = txtPlateColorName.Text.Trim();
drCurrent["Regtype"] = txtRegsource.Text.Trim();
drCurrent["vehicletype"] = txtVehType.Text.Trim();
drCurrent["vechiclemake"] = txtVehMake.Text.Trim();
drCurrent["vechiclemodel"] = txtVehModel.Text.Trim();
drCurrent["vechiclecolor"] = txtVehcolor.Text.Trim();
drCurrent["vechiclecolor1"] = txtVehColor1.Text.Trim();
drCurrent["Platecolor"] = txtPlateColorGcc.Text.Trim();
dt.Rows.Add(drCurrent);
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da);
da.Update(ds, "TBL_TXN_MAIN");
MessageBox.Show("Record saved successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
i click save that time Column 'FileID' does not allow nulls.then i remove primary key.. i change another name i given primary key that time database error came.Errors were encountered during the save process.how to solve this problem..
Posted 21-Jan-13 22:24pm
Comments
d@nish at 22-Jan-13 3:27am
   
1. Do not post entire code dump. Post relevant code.
2. Format the post using options available on top of text area.
3. Provide error details.
ravithejag at 22-Jan-13 3:36am
   
what is the error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here you are not passing fileid value to database.so it showing null error.also because that fileId column is primary key. One thing do ,put fileId column identification specification property to true.and identity incriment as 1.then the fileId value will insert automatically when succesful save.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

No cule what the error is in your source code. Maybe debugging your source code will help?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 185
3 Richard MacCutchan 100
4 DamithSL 65
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100