Whenever the code is executed, it always return that the ID value is not supplied that will cause an error. Would someone please show me where is my error? In some queries there is also sqlcommand.Parameters.Clear(); which is not included in this code.
The #ID should be pointing to the first column in the datagridview, where I can also set the match with the ID (primary key) in the table via SQL Management.
using (SqlCommand InsUpd = new SqlCommand(
#"IF EXISTS(SELECT 1 FROM dbo.Bokfuppg WHERE dbo.Bokfuppg.ID = #ID)
UPDATE dbo.bokfuppg
SET fnr = #Fnr, kto = #Kto, Ukto = #Ukto, avd = #Avd, prod = #Prod, proj = #Proj, ant = #Ant, bel = #Bel, Text = #Text, vbel = #Vbel, period = #Period
WHERE dbo.Bokfuppg.ID = #ID
ELSE
INSERT INTO dbo.bokfuppg(Fnr, Kto, Ukto, Avd, Prod, Proj, ant, bel, text, vbel, period) VALUES(#Fnr, #Kto, #Ukto, #Avd, #Prod, #Proj, #Ant, #Bel, #Text, #Vbel, #Period)", con))
{
con.Open();
using (SqlTransaction tr = con.BeginTransaction())
{
InsUpd.Transaction = tr;
InsUpd.Parameters.Add("#ID", SqlDbType.Int);
InsUpd.Parameters.Add("#Fnr", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Kto", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Ukto", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Avd", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Prod", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Proj", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Ant", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Bel", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Text", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Vbel", SqlDbType.NVarChar);
InsUpd.Parameters.Add("#Period", SqlDbType.NVarChar);
foreach (DataGridViewRow item in dataGridViewBokfuppg.Rows)
{
InsUpd.Parameters["#ID"].Value = item.Cells[0].Value;
InsUpd.Parameters["#Fnr"].Value = textBox1.Text;
InsUpd.Parameters["#Kto"].Value = item.Cells[1].Value;
InsUpd.Parameters["#Ukto"].Value = item.Cells[2].Value;
InsUpd.Parameters["#Avd"].Value = item.Cells[3].Value;
InsUpd.Parameters["#Prod"].Value = item.Cells[4].Value;
InsUpd.Parameters["#Proj"].Value = item.Cells[5].Value;
InsUpd.Parameters["#Ant"].Value = item.Cells[6].Value;
InsUpd.Parameters["#Bel"].Value = item.Cells[7].Value;
InsUpd.Parameters["#Text"].Value = item.Cells[8].Value;
InsUpd.Parameters["#Vbel"].Value = item.Cells[9].Value;
InsUpd.Parameters["#Period"].Value = item.Cells[10].Value;
InsUpd.ExecuteNonQuery();
}
tr.Commit();
}
}
MessageBox.Show("Record Updated Successfully");`
Related
I am trying to send arrays as parameter to Oracle stored proc in order to process bulk insert.
type Licensingentity_id is table of odilic_admin.licensingentity.licensingentity_id%type index by pls_integer;
type Nationalprovidernumber is table of odilic_admin.licensingentity.nationalprovidernumber%type index by pls_integer;
type Home_state_province_id is table of odilic_admin.licensingentity.home_state_province_id%type index by pls_integer;
procedure HomeStateLookup_bulk_insert(i_entityId in Licensingentity_id,
i_npn in Nationalprovidernumber,
i_homeStateId in Home_state_province_id) is
v_caller varchar2(60) := 'System_Scheduler';
begin
FORALL i IN 1 .. i_entityId.count
insert into home_state_lookup_stg
(licensingentity_id,
npn,
home_state_province_id,
isprocessed,
inserted_by,
inserted_date,
updated_by,
updated_date)
values
(i_entityId(i),
i_npn(i),
i_homeStateId(i),
0,
v_caller,
sysdate,
v_caller,
sysdate);
end HomeStateLookup_bulk_insert;
and here is the c# code
NiprConnectionString = ConfigurationManager.ConnectionStrings["ODI.NIPR.DB.Reader"].ConnectionString;
OracleConnection cnn = new OracleConnection(NiprConnectionString);
cnn.Open();
OracleCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = NaicStateLookupRepositoryProcedures.HOME_STATE_BULK_INSERT;
cmd.BindByName = true;
cmd.ArrayBindCount = entities.Count;
var i_entityId = new OracleParameter();
var i_npn = new OracleParameter();
var i_homeStateId = new OracleParameter();
i_entityId.OracleDbType = OracleDbType.Int32;
i_npn.OracleDbType = OracleDbType.Varchar2;
i_homeStateId.OracleDbType = OracleDbType.Int32;
i_entityId.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
i_npn.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
i_homeStateId.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
i_entityId.Value = entities.Select(c => c.Key).ToArray();
i_npn.Value = entities.Select(c => c.Value.Item1).ToArray();
i_homeStateId.Value = entities.Select(c => c.Value.Item2).ToArray();
i_entityId.Size = entities.Count;
i_npn.Size = entities.Count;
i_homeStateId.Size = entities.Count;
cmd.Parameters.Add(i_entityId);
//cmd.Parameters[0].Value = i_entityId;
cmd.Parameters.Add(i_npn);
//cmd.Parameters[1].Value = i_npn;
cmd.Parameters.Add(i_homeStateId);
//cmd.Parameters[2].Value = i_homeStateId;
int result = cmd.ExecuteNonQuery();
but getting an exception -
ORA-06550: line 1, column 52: PLS-00103: Encountered the symbol ">"
when expecting one of the following:
( ) - + case mod new not null
Any help is much appreciated.
I can't promise this is the answer, and I don't have the tables you reference to test this for myself, but at first glance I noticed you set:
cmd.BindByName = true;
As such, I think you need to declare your parameter names:
var i_entityId = new OracleParameter("i_entityId");
var i_npn = new OracleParameter("i_npn");
var i_homeStateId = new OracleParameter("i_homeStateId");
I've never passed an array as a parameter to a procedure, but if you were to do this with a normal insert, it would look something like this:
string sql = "insert into foo values (:boo, :bar, :baz)";
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.Parameters.Add(new OracleParameter("boo", OracleDbType.Varchar2));
cmd.Parameters.Add(new OracleParameter("bar", OracleDbType.Date));
cmd.Parameters.Add(new OracleParameter("baz", OracleDbType.Varchar2));
cmd.Parameters[0].Value = booArray;
cmd.Parameters[1].Value = barArray;
cmd.Parameters[2].Value = bazArray;
cmd.ArrayBindCount = booArray.Length;
cmd.ExecuteNonQuery();
I would start by defining the parameters with the parameter names, though.
I want to block the duplicate dates before inserting it into database.If a week with the same date is already exist in the database,when selecting the same date it should pop-up with the error message "this dates are already chosen".
Example:
If Week 1 is 1.1.2015 to 5.1.2015 stored in database and when Week 2 is also selecting the same date from 1.1.2015 to 5.1.2015,it should give the error message.
How to accomplish this,suggest some ideas.
This is my database.
I am using SQL database.Where the selected dates are stored in "datedif" column in database. With the "daywk" column I'm selecting the dates by week wise and displayed in grid.
I want to block the duplicate date when inserting into "datedif" column.
my code so far.
using (SqlConnection con2 = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=Z:\NewTimeTable\NewTimeTable\App_Data\Admindetail.mdf;Integrated Security=True"))
{
var fromdate = DateTime.Parse(txtfromdate.Text);
var todate = DateTime.Parse(txttodate.Text);
var datedif1 = (todate - fromdate).Days;
var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) VALUES (#datedif,#batch,#daywk,#semester,#weekbatch,#subject,#facultyname,#facultyid,#weekMonth)", con2);
var sqlParamater = sqlInsert.Parameters.Add("#datedif", SqlDbType.Date);
var sqlParameter1 = sqlInsert.Parameters.Add("#batch", SqlDbType.NVarChar);
var sqlParameter2 = sqlInsert.Parameters.Add("#daywk", SqlDbType.NVarChar);
var sqlParameter3 = sqlInsert.Parameters.Add("#semester", SqlDbType.NVarChar);
var sqlParameter4 = sqlInsert.Parameters.Add("#weekbatch", SqlDbType.NVarChar);
var sqlParameter5 = sqlInsert.Parameters.Add("#subject", SqlDbType.NVarChar);
var sqlParameter6 = sqlInsert.Parameters.Add("#facultyname", SqlDbType.NVarChar);
var sqlParameter7 = sqlInsert.Parameters.Add("#facultyid", SqlDbType.NVarChar);
var sqlParameter8 = sqlInsert.Parameters.Add("#WeekMonth", SqlDbType.NVarChar);
con2.Open();
for (var i = 0; i <= datedif1; i++)
{
var consecutiveDate = fromdate.AddDays(i);
sqlParamater.Value = consecutiveDate;
sqlParameter1.Value = batch1;
sqlParameter2.Value = dayweek;
sqlParameter3.Value = semester;
sqlParameter4.Value = weekbatch;
sqlParameter5.Value = subject;
sqlParameter6.Value = faculty;
sqlParameter7.Value = facultyid;
sqlParameter8.Value = weekmonth;
sqlInsert.ExecuteNonQuery();
}
con2.Close();
}
}
List persons = new List();
using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AdventureWorks2014;Integrated Security=SSPI"))
using (SqlCommand cmd = new SqlCommand("SELECT BusinessEntityID AS ID, FirstName, MiddleName, LastName FROM Person.Person", connection))
{
connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Check is the reader has any rows at all before starting to read.
if (reader.HasRows)
{
throw new ApplicationExcetion("Data is duplicated");
}
}
}
before insert into database. just query first.
if query have exist data(duplicated data). then throw exception
I have tried this below code, but it throws a unique key constraint error. I would like it as a pop-up message; see my below code.
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
var fromdate = DateTime.Parse(txtfromdate.Text);
var todate = DateTime.Parse(txttodate.Text);
var datedif1 = (todate - fromdate).Days;
var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) VALUES (#datedif,#batch,#daywk,#semester,#weekbatch,#subject,#facultyname,#facultyid,#weekMonth)", con2);
var sqlParamater = sqlInsert.Parameters.Add("#datedif", SqlDbType.Date);
var sqlParameter1 = sqlInsert.Parameters.Add("#batch", SqlDbType.NVarChar);
var sqlParameter2 = sqlInsert.Parameters.Add("#daywk", SqlDbType.NVarChar);
var sqlParameter3 = sqlInsert.Parameters.Add("#semester", SqlDbType.NVarChar);
var sqlParameter4 = sqlInsert.Parameters.Add("#weekbatch", SqlDbType.NVarChar);
var sqlParameter5 = sqlInsert.Parameters.Add("#subject", SqlDbType.NVarChar);
var sqlParameter6 = sqlInsert.Parameters.Add("#facultyname", SqlDbType.NVarChar);
var sqlParameter7 = sqlInsert.Parameters.Add("#facultyid", SqlDbType.NVarChar);
var sqlParameter8 = sqlInsert.Parameters.Add("#WeekMonth", SqlDbType.NVarChar);
con2.Open();
try
{
for (var i = 0; i <= datedif1; i++)
{
var consecutiveDate = fromdate.AddDays(i);
sqlParamater.Value = consecutiveDate;
sqlParameter1.Value = batch1;
sqlParameter2.Value = dayweek;
sqlParameter3.Value = semester;
sqlParameter4.Value = weekbatch;
sqlParameter5.Value = subject;
sqlParameter6.Value = faculty;
sqlParameter7.Value = facultyid;
sqlParameter8.Value = weekmonth;
int s = sqlInsert.ExecuteNonQuery();
}
con2.Close();
}
catch(ConstraintException ex)
{
throw new ApplicationException("data are duplicated");
}
}
Here's a screen shot of the error which am getting,
You are catching a ConstraintException, but the insert is throwing a SqlException which is not a subclass of ConstraintException. Change the catch and you will get it handled.
Here when I select same date and same batch it should give me the alert message with pop-up. I tried try catch method but it's not working.Tell me where I went wrong.
My code so far
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
var fromdate = DateTime.Parse(txtfromdate.Text);
var todate = DateTime.Parse(txttodate.Text);
var datedif1 = (todate - fromdate).Days;
var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) SELECT * from (SELECT #datedif as datedif, #batch as batch,#daywk as daywk,#semester as semster,#weekbatch as weekbatch,#subject as subject,#facultyname as facultyname,#facultyid as facultyid,#weekMonth as weekMonth) AS temp WHERE NOT EXISTS (SELECT datedif,batch from datelist where batch=#batch and datedif=#datedif)", con2);
var sqlParamater = sqlInsert.Parameters.Add("#datedif", SqlDbType.Date);
var sqlParameter1 = sqlInsert.Parameters.Add("#batch", SqlDbType.NVarChar);
var sqlParameter2 = sqlInsert.Parameters.Add("#daywk", SqlDbType.NVarChar);
var sqlParameter3 = sqlInsert.Parameters.Add("#semester", SqlDbType.NVarChar);
var sqlParameter4 = sqlInsert.Parameters.Add("#weekbatch", SqlDbType.NVarChar);
var sqlParameter5 = sqlInsert.Parameters.Add("#subject", SqlDbType.NVarChar);
var sqlParameter6 = sqlInsert.Parameters.Add("#facultyname", SqlDbType.NVarChar);
var sqlParameter7 = sqlInsert.Parameters.Add("#facultyid", SqlDbType.NVarChar);
var sqlParameter8 = sqlInsert.Parameters.Add("#WeekMonth", SqlDbType.NVarChar);
con2.Open();
try
{
for (var i = 0; i <= datedif1; i++)
{
var consecutiveDate = fromdate.AddDays(i);
sqlParamater.Value = consecutiveDate;
sqlParameter1.Value = batch1;
sqlParameter2.Value = dayweek;
sqlParameter3.Value = semester;
sqlParameter4.Value = weekbatch;
sqlParameter5.Value = subject;
sqlParameter6.Value = faculty;
sqlParameter7.Value = facultyid;
sqlParameter8.Value = weekmonth;
int s = sqlInsert.ExecuteNonQuery();
}
}
catch
{
Response.Write("<script>alert('Already date is selected.Select another date')</script>");
con2.Close();
}
}
}
1 - Please first check are you going inside catch block put a debugger there.
2 - Put a debugger also on your page load to check when it is happening.
You can use RegisterStartupScript() methodAt Server side
catch
{
string msg="Already date is selected.Select another date";
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "myFunction('"+msg+"');", true);
}
At Client side
<script>
function myFunction(a) {
alert(a);
}
</script>
Also it is better to make some store procedure.
I am trying to write a code do payment and update the status as well. In the following code I have done the logic to submit the payment which is working correctly but I am not able to update the status. Status is a column in Item_Order table which must get update after do the payment
My code is :
delegate void PaymentDone();
public void paySave()
{
SqlConnection conn1 = new SqlConnection();
try
{
conn1.ConnectionString = "
server=.\\ms2k5;database=Info_Connect;Trusted_Connection=true";
conn1.Open();
SqlCommand insertPayment = new SqlCommand("Delegate_SP", conn1);
insertPayment.CommandType = CommandType.StoredProcedure;
string currentDate = DateTime.Today.ToString();
if (Convert.ToDateTime(payDate.Text) >= Convert.ToDateTime(currentDate))
{
insertPayment.Parameters.Add("#reciptId", SqlDbType.VarChar);
insertPayment.Parameters["#reciptId"].Value = fullfill.Text;
insertPayment.Parameters.Add("#payDate", SqlDbType.DateTime);
insertPayment.Parameters["#payDate"].Value =
Convert.ToDateTime(payDate.Text);
insertPayment.Parameters.Add("#balPay", SqlDbType.Float);
insertPayment.Parameters["#balPay"].Value = Convert.ToDouble(balace.Text) - Convert.ToDouble(amnt1.Text);
insertPayment.Parameters.Add("#payDone", SqlDbType.Float);
insertPayment.Parameters["#payDone"].Value =
Convert.ToDouble(amnt1.Text);
insertPayment.Parameters.Add("#fullfillID_FK", SqlDbType.VarChar);
insertPayment.Parameters["#fullfillID_FK"].Value = fullfill.Text;
insertPayment.Parameters.Add("#clintID_FK", SqlDbType.Int);
insertPayment.Parameters["#clintID_FK"].Value =
(clintId_FK.Text).ToString();
insertPayment.Parameters.Add("#operation", SqlDbType.VarChar);
insertPayment.Parameters["#operation"].Value = 3;
insertPayment.ExecuteNonQuery();
}
else
{
MessageBox.Show("Payment Date Should be later than Full Order Date");
}
}
}
public void payUpdate()
{
SqlConnection conn1 = new SqlConnection();
try
{
conn1.ConnectionString = "
server=.\\ms2k5;database=Info_Connect;Trusted_Connection=true";
conn1.Open();
SqlCommand insertStaus = new SqlCommand("Delegate_SP", conn1);
insertStaus.CommandType = CommandType.StoredProcedure;
insertStaus.Parameters.Add("#status", SqlDbType.VarChar);
insertStaus.Parameters["#status"].Value = "RUNING";
if (balace.Text.Equals("0"))
{
insertStaus.Parameters.Add("#status", SqlDbType.VarChar);
insertStaus.Parameters["#status"].Value = "CLOSE";
}
else
{
insertStaus.Parameters.Add("#status", SqlDbType.VarChar);
insertStaus.Parameters["#status"].Value = "RUNING";
}
MessageBox.Show("3");
insertStaus.Parameters.Add("#operation", SqlDbType.VarChar);
insertStaus.Parameters["#operation"].Value = 2;
insertStaus.ExecuteNonQuery();
}
}
And my stored procedure is
ALTER PROCEDURE [dbo].[Delegate_SP]
#reciptId varchar(50)=null,
#balPay float,
#payDone float=null,
#payDate datetime=null,
#fullfillId_FK varchar(50)=null,
#clintId_FK int=null,
#status varchar(50)=null,
#operation int,
#fullfillId varchar(50)=null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
if #operation=3
BEGIN
UPDATE Recipt set balPay=#balPay where reciptId=#reciptId
if ##rowcount=0
insert into Recipt(reciptId,balPay,payDone,payDate,fullfillId_FK,clintId_FK)values
(#reciptId,#balPay,#payDone,#payDate,#fullfillId_FK,#clintId_FK)
update Item_Full set totCost=(select balPay from Recipt) where fullfillId=#reciptId
END
if #operation=2
BEGIN
UPDATE Item_Order set status=#status where orderId=#reciptId
END
END
the problem is that I got tdsparser exception in payUpdate method... the paySave method is working correctly