Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
private void btnsave_Click(object sender, EventArgs e)
        {
            String query = "insert into Accounts(Account_No,Account_title,Entry_Date,Initial_Deposite,Account_type,Account_Nature,Postal_Code,District,City,Name,Father_name,Husband_name)values(@Account_No,@Account_title,@Entry_Date,@Initial_Deposite,@Account_type,@Account_Nature,@Postal_Code,@District,@city,@Name,@Father_name,@Husband_name,@Occupation,@Mobile,@CNIC)";
            SqlCommand cmd = new SqlCommand(query, cn);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@Account_No", txtaccount1.Text);
            //cmd.Parameters.Add("@Account_No", System.Data.SqlDbType.NVarChar).Value = txtaccount1.Text;
            cmd.Parameters.AddWithValue("@Account_title", txtaccounttitle.Text);
           // cmd.Parameters.Add("@Account_title", System.Data.SqlDbType.NVarChar).Value = txtaccounttitle.Text;
            cmd.Parameters.AddWithValue("@Entry_Date", dateTimePicker1.Value.Date);
            cmd.Parameters.AddWithValue("@Initial_Deposite", txtinitial.Text);
            //cmd.Parameters.AddWithValue("@Account_type", txtaccounttype.Text);
            cmd.Parameters.AddWithValue("@Account_Nature", txtaccountnature.Text);
            cmd.Parameters.AddWithValue("@Account_type",cboaccounttype.SelectedValue);
          
            cmd.Parameters.AddWithValue("@Postal_code", txtpostal.Text);
            cmd.Parameters.AddWithValue("@City", cbocity.SelectedValue);
            cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedValue);
                
            cmd.Parameters.AddWithValue("@Name", txtname1.Text);
            cmd.Parameters.AddWithValue("@Father_name", txtfather1.Text);
            cmd.Parameters.AddWithValue("@Husband_name", txthusband1.Text);
            cmd.Parameters.AddWithValue("@Occupation", txtoccupation1.Text);
            cmd.Parameters.AddWithValue("@Mobile", txtmobile1.Text);
            cmd.Parameters.AddWithValue("@CNIC", txtcnic1.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("record stored");
                   
        }
            }
        }
Posted
Updated 2-Oct-12 4:06am
v2

1 solution

Check the value of cboaccounttype.SelectedValue - ifg it is null then you want to pass DBNull.Value instead.
You should be able to use the null coalescence operator for that:
C#
cmd.Parameters.AddWithValue("@Account_type",cboaccounttype.SelectedValue ?? DBNull.Value);
 
Share this answer
 
Comments
zeshanazam 2-Oct-12 10:47am    
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
zeshanazam 2-Oct-12 10:52am    
Plz help with this
zeshanazam 2-Oct-12 11:29am    
combo box value coloumn are null.....values are not entering...plZ help me with this.....
Thanks in advance...
OriginalGriff 2-Oct-12 12:14pm    
The null values are as above, but a quick check says there are fewer column name than parameters:
Account_No, Account_title, Entry_Date, Initial_Deposite, Account_type, Account_Nature,
@Account_No,@Account_title,@Entry_Date,@Initial_Deposite,@Account_type,@Account_Nature,

Postal_Code, District, City, Name, Father_name, Husband_name
@Postal_Code,@District,@city,@Name,@Father_name,@Husband_name,@Occupation,@Mobile,@CNIC

So you need to add column names for Occupation, Mobile, and CNIC, or remove the parameters
zeshanazam 2-Oct-12 13:49pm    
i want to insert values to sql database through combo box but due to
cmd.Parameters.AddWithValue("@Account_type",cboaccounttype.SelectedValue ?? DBNull.Value);
sql database coloumn of account type is null...plz help me with this...
thanks in advance..... i m new in c#

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