Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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,Occupation,Mobile,CNIC,Gender)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,@Gender)";
            SqlCommand cmd = new SqlCommand(query, cn);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@Account_No", txtaccount1.Text);

            cmd.Parameters.AddWithValue("@Account_title", txtaccounttitle.Text);

            cmd.Parameters.AddWithValue("@Entry_Date", dateTimePicker1.Value.Date);
            cmd.Parameters.AddWithValue("@Initial_Deposite", txtinitial.Text + "/-");
            cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedItem);
            cmd.Parameters.AddWithValue("@Account_Nature", cboaccountnature.SelectedItem);

            cmd.Parameters.AddWithValue("@Postal_code", txtpostal.Text);

            cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedItem);
            cmd.Parameters.AddWithValue("@City", cbocity.SelectedItem);
            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", cbocode.SelectedItem + "-" + txtmobile1.Text);
            cmd.Parameters.AddWithValue("@CNIC", txtcnic1.Text + "-" + txtcnic2.Text + "-" + txtcnic3.Text);
            if (rdomale.Checked == true)
                cmd.Parameters.AddWithValue("@Gender", rdomale.Text);
            else if (rdofemale.Checked == true)
                cmd.Parameters.AddWithValue("@Gender", rdofemale.Text);
            else


                cmd.ExecuteNonQuery();
            MessageBox.Show("Record Stored Successfully", "Success Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

[Edit]Code block added[/Edit]
Posted
Updated 17-Oct-12 8:00am
v2

1 solution

Call cmd.Prepare(); after you assign parameters values ie just before cmd.ExecuteNonQuery()..
Also if you call an Execute method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.

Prepare() is normally used with Stored Procedures, and my suggestion is remove it and execute.

Ref: http://msdn.microsoft.com/en-us/library/wbys3e9s.aspx[^]
 
Share this answer
 
v2
Comments
zeshanazam 17-Oct-12 14:32pm    
got an error "SqlCommand.Prepare method requires all parameters to have an explicitly set type.
Kuthuparakkal 17-Oct-12 14:36pm    
Like this : Parameters.Add(new SqlParameter("@Account_No", SqlDbType.Int));
Kuthuparakkal 17-Oct-12 14:39pm    
http://msdn.microsoft.com/en-us/library/wbys3e9s.aspx
zeshanazam 17-Oct-12 14:39pm    
actually there is no error in code,, but in sql database, the account table remain empty...no record inserted
fjdiewornncalwe 17-Oct-12 14:48pm    
Wrong. The error will be in your code because it is not executing in the database.

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900