Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

In My database there are few columns as

Empid(Pk,int,not null)
Deptid (int, null),
DeptName(varchar(50),null),
Location(varchar(50),null)

From the front end..I am displaying a combobox where the Display member is DeptName and ValueMember is DeptID.Combobox intial value is --Select--

So when user doesn't select any item from the combobox I want to insert/update database columns with null value.

I tried to do it by converting the selectedvalue to int and when the user doesn't select an item.It throws an error as it is null.

So how to insert null.
Posted

 
Share this answer
 
Comments
Prathap Gangireddy 25-Dec-12 6:03am    
Hi Krunal,

Thanks for the reply, but my problem is I am using a stored procedure for Inserting/Updating the table in Nlayer application.

objEmployeeDetailsMaster.DeptID = Convert.ToInt32(cmbDepartmenttype.SelectedValue.ToString());

As you can see when I am trying to get the selectedvalue it throws an error as I am not selecting an item.

Can u please help me with this.
[no name] 25-Dec-12 6:10am    
Okay, check one condition that cmbDepartmenttype.SelectedIndex==0 then, your desire code....
Prathap Gangireddy 25-Dec-12 6:13am    
Actually it is not compulsory that user must select a value.So when slectedindex=0 what shud I do in order to store null value into database.Only option I got is set the value=0 when selectedindex=0 so that we can insert 0 in the database.
[no name] 25-Dec-12 6:16am    
You are not getting me, see
if(cmbDepartmenttype.SelectedIndex==0)
{
// write query for inserting NULL
}
else
{
//query containing your selected item.
}
Prathap Gangireddy 25-Dec-12 6:21am    
Another problem is..i have just asked you a sample of code.In actualy application there are many comboboxes which have Display nd Value member.So finally all the selected data is entered into a Master table.How can I do this.
check combobox value and if it's Value is '--Select--' then
use Convert.DBNull instead of combobox.Value
 
Share this answer
 
Comments
Prathap Gangireddy 25-Dec-12 6:36am    
Hi Gagan,

thanks for the reply.Can you please elaborate on this.
gagan sawaliya 25-Dec-12 7:00am    
pls post your code
Prathap Gangireddy 25-Dec-12 7:10am    
Below is my SaveButton code in UI which inserts the values into DB

private void btnEmpDSave_Click(object sender, EventArgs e)
{

Restaurant.Entities.EmployeeDetailsMaster objEmployeeDetailsMaster = new Restaurant.Entities.EmployeeDetailsMaster();

if (DateofJoining.Value <= DateofLeaving.Value)
{
if (btnEmpDSave.Text.Equals("Save"))
{
lblEmpID.Text = "0";
}
objEmployeeDetailsMaster.EmpID = Convert.ToInt32(lblEmpID.Text);
objEmployeeDetailsMaster.Address1 = txtAddress1.Text.Trim();
objEmployeeDetailsMaster.Address2 = txtAddress2.Text.Trim();
objEmployeeDetailsMaster.Age = Convert.ToInt32(txtAge.Text.ToString());
objEmployeeDetailsMaster.City = txtcity.Text.Trim();
objEmployeeDetailsMaster.Comments = txtComments.Text.Trim();
objEmployeeDetailsMaster.Company_ID = GlobalData.CompanyID;
objEmployeeDetailsMaster.CountryID = Convert.ToInt32(cmbCountry.SelectedValue.ToString());
objEmployeeDetailsMaster.Dateleft = DateofLeaving.Value;
objEmployeeDetailsMaster.DateofJoin = DateofJoining.Value;
objEmployeeDetailsMaster.DeptID = Convert.ToInt32(cmbDepartmenttype.SelectedValue.ToString());
objEmployeeDetailsMaster.DesignationID = Convert.ToInt32(cmbDesignation.SelectedValue.ToString());
objEmployeeDetailsMaster.DOB = DOBdatetimepicker.Value;
objEmployeeDetailsMaster.Email = txtEmail.Text.Trim();
objEmployeeDetailsMaster.EmpEthnicOriginID = Convert.ToInt32(cmbEthnicOrigin.SelectedValue.ToString());
objEmployeeDetailsMaster.EmpImagepath = string.Empty;
objEmployeeDetailsMaster.EmpNo = txtEmpNO.Text.Trim();
objEmployeeDetailsMaster.EmpstatusID = Convert.ToInt32(cmbEmpStatus.SelectedValue.ToString());
objEmployeeDetailsMaster.Emptitleid = Convert.ToInt32(cmbEmptitle.SelectedValue.ToString());
objEmployeeDetailsMaster.EmpTypeID = Convert.ToInt32(cmbEmpType.SelectedValue.ToString());
objEmployeeDetailsMaster.Empwebsite = string.Empty;
objEmployeeDetailsMaster.Fax1 = txtFax1.Text.Trim();
objEmployeeDetailsMaster.Fax2 = txtFax2.Text.Trim();
objEmployeeDetailsMaster.Firstname = txtFirstName.Text.Trim();
objEmployeeDetailsMaster.GenderID = Convert.ToInt32(cmbGenderType.SelectedValue.ToString());
objEmployeeDetailsMaster.LocationID = Convert.ToInt32(cmbLocation.SelectedValue.ToString());
objEmployeeDetailsMaster.LocationGroupId = Convert.ToInt32(cmbLocationGroup.SelectedValue.ToString());
objEmployeeDetailsMaster.Mobile1 = txtMobile1.Text.Trim();
objEmployeeDetailsMaster.Mobile2 = txtMobile2.Text.Trim();
objEmployeeDetailsMaster.Phone1 = txtPhone1.Text.Trim();
objEmployeeDetailsMaster.Phone2 = txtPhone2.Text.Trim();
objEmployeeDetailsMaster.RecruitmentTypeID = Convert.ToInt32(cmbRecruitmenttype.SelectedValue.ToString());
objEmployeeDetailsMaster.ReportingAuthorityempid = Convert.ToInt32(cmbEmpRAuthority.SelectedValue.ToString());
objEmployeeDetailsMaster.ReporttoempID = Convert.ToInt32(cmbEmpReport.SelectedValue.ToString());
objEmployeeDetailsMaster.State = txtState.Text.Trim();
objEmployeeDetailsMaster.Surname = txtSurName.Text.Trim();

using (EmployeeMasterBusiness objEmployeeMasterBusiness = new EmployeeMasterBusiness())
{
if (objEmployeeMasterBusiness.SaveRUpdateEmpMasterDetails(objEmployeeDetailsMaster))
{
MessageBox.Show("Employee details created/
gagan sawaliya 25-Dec-12 7:29am    
if (cmbDepartmenttype.SelectedValue == "--Select--")
{
objEmployeeDetailsMaster.DeptID = -1;
}
else
{
objEmployeeDetailsMaster.DeptID = Convert.ToInt32(cmbDepartmenttype.SelectedValue.ToString());
}


IN save method objEmployeeMasterBusiness.SaveRUpdateEmpMasterDetails(objEmployeeDetailsMaster)

check the value of objEmployeeDetailsMaster.DeptID if it's value is -1 then use Convert.DBNull else use objEmployeeDetailsMaster.DeptID
Prathap Gangireddy 25-Dec-12 8:46am    
Hi Gagan,

The only i didn't understand is how to use Convert.DBNULL.

I had used in the Business layer method i.e SaveRUpdateEmpMasterDetails as below but it throws error

if (EDMData.DeptID== -1)
EDMData.DeptID= Convert.DBNull;

Can you please help me with this

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