I've been under some problems regarding data type nvarchar
to numeric
error. Since I'm still beginning with SQL and C#, I was wondering if someone could lead me to some valid conclusions.
SQL code:
CREATE TABLE [dbo].[CustomerMembership]
(
[CM_ID] VARCHAR(255) NOT NULL,
[CustomerID] INT NOT NULL,
[MembershipID] INT NOT NULL,
[DateFrom] DATETIME NOT NULL,
[Months] VARCHAR(2) NOT NULL,
[DateTo] DATETIME NOT NULL,
[ChargesPerMonth] DECIMAL(18,2) NOT NULL,
[DiscountPer] DECIMAL(3,0) NOT NULL,
[VATPer] DECIMAL(3,0) NOT NULL,
[VATAmount] DECIMAL(3,0) NOT NULL,
[ServiceTaxPer] DECIMAL(3,0) NOT NULL,
[ServiceTaxAmount] DECIMAL(3,0) NOT NULL,
[TotalCharges] DECIMAL(18,2) NOT NULL,
[BillDate] DATETIME NOT NULL,
[SubTotal] DECIMAL(18,2) NOT NULL,
[GrandTotal] DECIMAL(18,2) NOT NULL,
[TotalPaid] DECIMAL(18,2) NOT NULL,
[DiscountAmount] DECIMAL(3,0) NOT NULL,
CONSTRAINT [PK_CustomerMembership]
PRIMARY KEY CLUSTERED ([CM_ID] ASC)
)
C# code:
cb = @"insert into CustomerMembership
(CM_ID,BillDate,CustomerID,MembershipID,DateFrom,
Months,DateTo,ChargesPerMonth,TotalCharges,
DiscountPer,DiscountAmount,SubTotal,VATPer,VATAmount,
ServiceTaxPer,ServiceTaxAmount,GrandTotal,TotalPaid) VALUES
(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,
@d12,@d13,@d14,@d15,@d16,@d17,@d18)";
cc.cmd = new SqlCommand(cb);
cc.cmd.Connection = cc.con;
cc.cmd.Parameters.AddWithValue("@d1",txtMembershipID.Text);
cc.cmd.Parameters.AddWithValue("@d2", dtpBillDate.Value);
cc.cmd.Parameters.AddWithValue("@d3", txtM_ID.Text);
cc.cmd.Parameters.AddWithValue("@d4", txtMembershipTypeID.Text);
cc.cmd.Parameters.AddWithValue("@d5", dtpDateFrom.Value);
cc.cmd.Parameters.AddWithValue("@d6", txtMonths.Text);
cc.cmd.Parameters.AddWithValue("@d7", dtpDateTo.Value);
cc.cmd.Parameters.AddWithValue("@d8", txtChargesPerMonth.Text);
cc.cmd.Parameters.AddWithValue("@d9", txtTotalCharges.Text);
cc.cmd.Parameters.AddWithValue("@d10", txtDiscountPer.Text);
cc.cmd.Parameters.AddWithValue("@d11", txtDiscountAmount.Text);
cc.cmd.Parameters.AddWithValue("@d12", txtSubTotal.Text);
cc.cmd.Parameters.AddWithValue("@d13", txtVATPer.Text);
cc.cmd.Parameters.AddWithValue("@d14", txtVATAmount.Text);
cc.cmd.Parameters.AddWithValue("@d15", txtServiceTaxPer.Text);
cc.cmd.Parameters.AddWithValue("@d16", txtServiceTaxAmount.Text);
cc.cmd.Parameters.AddWithValue("@d17", txtGrandTotal.Text);
cc.cmd.Parameters.AddWithValue("@d18", txtTotalPaid.Text);
cc.cmd.ExecuteReader();
cc.con.Close();
st1 = lblUser.Text;
st2 = "Foi adicionada uma nova mensalidade com o id '" + txtMembershipID.Text + "' ao membro '" + txtMemberName.Text + "'";
cf.LogFunc(st1, System.DateTime.Now, st2);
btnSave.Enabled = false;
MessageBox.Show("Gravado com sucesso", "Registo", MessageBoxButtons.OK, MessageBoxIcon.Information);
After the comments:
cc.con = new SqlConnection(cs.DBConn);
cc.con.Open();
string cb = "insert into CustomerMembership
(CM_ID,BillDate,CustomerID,MembershipID,DateFrom,Months,DateFrom,
ChargesPerMonth,TotalCharges,DiscountPer,DiscountAmount,
SubTotal,VATPer,VATAmount,ServiceTaxPer,
ServiceTaxAmount,GrandTotal,TotalPaid) VALUES
(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,
@d13,@d14,@d15,@d16,@d17,@d18)";
cc.cmd = new SqlCommand(cb);
cc.cmd.Connection = cc.con;
cc.cmd.Parameters.Add("@d1", SqlDbType.Int, 50).Value = txtMembershipID.Text ;
cc.cmd.Parameters.Add("@d2", SqlDbType.DateTime).Value = dtpBillDate.Value;
cc.cmd.Parameters.Add("@d3", SqlDbType.VarChar, 50).Value = txtM_ID.Text;
cc.cmd.Parameters.Add("@d4", SqlDbType.VarChar, 50).Value = txtMembershipTypeID.Text;
cc.cmd.Parameters.Add("@d5", SqlDbType.DateTime).Value = dtpDateFrom.Value;
cc.cmd.Parameters.Add("@d6", SqlDbType.VarChar, 2).Value = txtMonths.Text;
cc.cmd.Parameters.Add("@d7", SqlDbType.DateTime).Value = dtpDateTo.Value;
cc.cmd.Parameters.Add("@d8", SqlDbType.VarChar, 50).Value = txtChargesPerMonth.Text;
cc.cmd.Parameters.Add("@d9", SqlDbType.VarChar, 50).Value = txtTotalCharges.Text;
cc.cmd.Parameters.Add("@d10", SqlDbType.VarChar, 50).Value = txtDiscountPer.Text;
cc.cmd.Parameters.Add("@d11", SqlDbType.VarChar, 50).Value = txtDiscountAmount.Text;
cc.cmd.Parameters.Add("@d12", SqlDbType.VarChar, 50).Value = txtSubTotal.Text;
cc.cmd.Parameters.Add("@d13", SqlDbType.VarChar, 50).Value = txtVATPer.Text;
cc.cmd.Parameters.Add("@d14", SqlDbType.VarChar, 50).Value = txtVATAmount.Text;
cc.cmd.Parameters.Add("@d15", SqlDbType.VarChar, 50).Value = txtServiceTaxPer.Text;
cc.cmd.Parameters.Add("@d16", SqlDbType.VarChar, 50).Value = txtServiceTaxAmount.Text;
cc.cmd.Parameters.Add("@d17", SqlDbType.VarChar, 50).Value = txtGrandTotal.Text;
cc.cmd.Parameters.Add("@d18", SqlDbType.VarChar, 50).Value = txtTotalPaid.Text;
cc.cmd.ExecuteReader();
cc.con.Close();
st1 = lblUser.Text;
st2 = "Foi adicionada uma nova mensalidade com o id '" + txtMembershipID.Text + "' ao membro '" + txtMemberName.Text + "'";
cf.LogFunc(st1, System.DateTime.Now, st2);
btnSave.Enabled = false;
MessageBox.Show("Gravado com sucesso", "Registo", MessageBoxButtons.OK, MessageBoxIcon.Information);
Answering your updated code:
Your line
cc.cmd.Parameters.Add("@d1", SqlDbType.Int, 50).Value = txtMembershipID.Text ;
is expecting a int
to be passed in, you are passing in a string from the text box. You need to convert the string to a int before you assign the value.
//This can throw a exception if the text is not a valid int.
cc.cmd.Parameters.Add("@d1", SqlDbType.Int).Value = int.Parse(txtMembershipID.Text) ;
The same thing needs to be done for all your decimal
and datetime
entries too for their respective data types.
You need to pass parameters of the exact datatype expected by the columns that you want to update. For example the column CustomerID in the datatable is of type INT. The parameter for this field should be an integer
Consider also that all these conversions should be done with appropriate check against the user input. You cannot trust your user to type exactly a number when you expect a number.
.... string columns could get directly from the Text property
.... date columns need a DateTime parameter as well as int columns
int memberID;
if(!Int32.TryParse(txtMembershipID.Text, out memberID))
{
MessageBox.Show("Invalid number");
return;
}
cc.cmd.Parameters.Add("@d3", SqlDbType.Int).Value = memberID;
Also, to avoid confusion with all these parameter placeholders with an quasi-identical name, I suggest to name the parameters in a more recognizable way. You could use the same name of the column with the @ prefix. So when you set the values you have a more clear view of your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.