Click here to Skip to main content
15,889,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
try
{
    if (txtDesc1.Text == "")
    {
    }
    else
    {
        con1 = new SqlDbConnect();
        strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
        strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
        strSQL += ", '" + txtDesc1.Text.Replace("'", "''") + "'";
        strSQL += ", '" + txtDosage1.Text.Replace("'", "''") + "'";
        strSQL += ", '" + txtQty1.Text.Replace("'", "''") + "'";
        strSQL += ", '" + txtSelling1.Text.Replace("'", "''") + "'";
        strSQL += ", '" + txtAmount1.Text.Replace("'", "''") + "'";
        con1.SqlQuery(strSQL);
        con1.NonQueryEx();
        con1.Close();
    }


this is my btnSave_click code

i had a table StockBin
description nvchar50
dosage nvchar50
plus int
minus int
stockleft int

My question is when i am clicking to save this to database
txtQty1 will also update to my stockbin table as minus

Thanks in advance
Posted
Updated 24-Apr-14 15:22pm
v2
Comments
[no name] 24-Apr-14 21:29pm    
Well you are lucky you are not my student, I would flunk you if you turned this in. Show us the actual query.
Member 10717094 24-Apr-14 22:59pm    
I am just asking question well if you do not have some tips or some helpful answer then shut up.
[no name] 25-Apr-14 7:14am    
Well it's a really good thing that you can't tell me what to do. You have already been told what to do, why this code is horrible and how to code it correctly. So since you cannot listen, what would be the use of telling you again?
Member 10717094 25-Apr-14 16:28pm    
this is just my hobby and i am really very sorry if i got in your way. no offense sir Wes, anyway thanks for your time. And thanks also to every other people who really helps newbie in c#. Salute you guys :)
[no name] 26-Apr-14 7:08am    
No offense taken, I simply don't help rude people. You can't help people that don't want to be helped. And you don't want to be helped.

First of all, please don't use concatenation to form the statement; use parameters:

Simplified Database Access via ADO.NET Interfaces[^]

Plus, if you can execute one statement, you can execute two, in a transaction is prefered.

If this is SQL Server, you can actually pass multiple statements at once if you like. Or you might choose to write a stored procedure to do it.
 
Share this answer
 
sqlCon.Open();
SqlCommand cmd = new SqlCommand("procDeliveryStock", sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", labelID.Text));
cmd.Parameters.Add(new SqlParameter("@date", dateTimePicker1.Text));
cmd.Parameters.Add(new SqlParameter("@description", txtItemDescription.Text));
cmd.Parameters.Add(new SqlParameter("@dosage", txtDosage.Text));
cmd.Parameters.Add(new SqlParameter("@qty", txtQty.Text));
cmd.Parameters.Add(new SqlParameter("@selling", txtSelling.Text));
cmd.Parameters.Add(new SqlParameter("@receiving", txtReceiving.Text));
cmd.Parameters.Add(new SqlParameter("@plus", txtQty.Text));
sqlCon.Close();

and heres my Store Procedure

USE [BotikaNiChauncey]
GO
/****** Object: StoredProcedure [dbo].[procDeliveryStock] Script Date: 04/26/2014 04:22:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[procDeliveryStock]
(
@id INT, @date DATETIME,
@description VARCHAR(50),
@dosage VARCHAR(50),
@selling MONEY,
@receiving MONEY,
@qty INT,
@plus INT,
@minus INT,
@stock INT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION
INSERT INTO Delivery
(
id, date, description, dosage, qty, selling, receiving
)
values
(
@id, @date, @description, @dosage, @qty, @selling, @receiving
)
IF @@ERROR = 0
BEGIN
INSERT INTO StockCard
(
descripton,dosage,[+],[-], stock
)
VALUES
(
@description, @dosage, @plus, @minus, @stock
)
IF @@ERROR = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
BEGIN
ROLLBACK TRANSACTION
END
END
ELSE
BEGIN
ROLLBACK TRANSACTION
END

END

My problem is it is not saving in the database but there is no error on runtime.

Thanks in Advance
 
Share this answer
 

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