I'm writing a c# program and using SQL Server as a database.
I'm running an INSERT
, SELECT
, and UPDATE
followed by each other.
The first two statements run okay but the last one, the UPDATE
statement, will run once but when it runs the second time it will freeze the program and after 30 seconds or so I get this error:
System.Data.SqlClient.SqlException: 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.
Inner Exception
Win32Exception: The wait operation timed out
this is my connection string:
private const string constr = @"Data Source=MSI\SQLEXPRESS;Initial Catalog=DatabaseHuskyCenter;Integrated Security=True ;MultipleActiveResultSets=True";
and this is the table of Inventory:
USE [DatabaseHuskyCenter]
GO
/****** Object: Table [dbo].[Inventory] Script Date: 12/21/2020 20:07:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Inventory](
[IDinventory] [nvarchar](50) NOT NULL,
[IDitem] [nvarchar](50) NULL,
[quantity] [int] NULL,
CONSTRAINT [PK_Inventory] PRIMARY KEY CLUSTERED
(
[IDinventory] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Inventory] WITH CHECK ADD CONSTRAINT [FK_Inventory_Items] FOREIGN KEY([IDitem])
REFERENCES [dbo].[Items] ([IDitem])
GO
ALTER TABLE [dbo].[Inventory] CHECK CONSTRAINT [FK_Inventory_Items]
GO
This is my application code that runs the three queries:
int quan = 1;
int m=0;
string tag = ((PictureBox)sender).Tag.ToString();
sqlcon = new Connection();
sqlcon.cmd = new SqlCommand("select * from Inventory,Items where Inventory.IDitem = Items.IDitem and Items.IDitem like '" + tag + "'", sqlcon.con);
sqlcon.dr = sqlcon.cmd.ExecuteReader();
sqlcon.dr.Read();
if (sqlcon.dr.HasRows)
{
sqlcon.cmd = new SqlCommand("INSERT INTO SellActions(ID,IDsell,IDitem,IDuser,buyerName,[Date],quantityyy,[index])values(@id,@idsell,@iditem,@iduser,@buyer,@date,@quan,@index)", sqlcon.con);
sqlcon.cmd.Parameters.AddWithValue("@id", sqlcon.SellActionCode());
sqlcon.cmd.Parameters.AddWithValue("@idsell",codeTxt.Text);
sqlcon.cmd.Parameters.AddWithValue("@iditem", tag);
sqlcon.cmd.Parameters.AddWithValue("@iduser", sqlcon.getUserID());
sqlcon.cmd.Parameters.AddWithValue("@buyer", nameTxt.Text);
sqlcon.cmd.Parameters.AddWithValue("@date", dateandtime.Dateshort());
sqlcon.cmd.Parameters.AddWithValue("@quan", quan);
sqlcon.cmd.Parameters.AddWithValue("@index", index++);
try
{
sqlcon.cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
throw;
}
sqlcon.cmd.Dispose();
SqlCommand find = new SqlCommand("select quantity from Inventory where IDitem = '" + tag+ "'", sqlcon.con);
try
{
sqlcon.dr = find.ExecuteReader();
while (sqlcon.dr.Read())
{
m =sqlcon.dr.GetInt32(0);
}
sqlcon.dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
find.Dispose();
sqlcon.dr.Close();
m -= quan;
MessageBox.Show(m.ToString());
if (sqlcon.con.State == ConnectionState.Closed)
{
sqlcon.con.Open();
}
sqlcon.cmd = new SqlCommand("update Inventory set quantity=@quantity where IDitem = @tag", sqlcon.con);
sqlcon.cmd.Parameters.AddWithValue("@tag", tag.ToString());
sqlcon.cmd.Parameters.AddWithValue("@quantity", m.ToString());
try
{
sqlcon.cmd.ExecuteNonQuery();
sqlcon.cmd.Dispose();
sqlcon.con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
throw;
}
Clear();
getData();
}
sqlcon.dr.Close();
ItemLimit++;
its quite a mess, sorry about that but the problem occurs here
if (sqlcon.con.State == ConnectionState.Closed)
{
sqlcon.con.Open();
}
sqlcon.cmd = new SqlCommand("update Inventory set quantity=@quantity where IDitem = @tag", sqlcon.con);
sqlcon.cmd.Parameters.AddWithValue("@tag", tag.ToString());
sqlcon.cmd.Parameters.AddWithValue("@quantity", m.ToString());
try
{
sqlcon.cmd.ExecuteNonQuery();
sqlcon.cmd.Dispose();
sqlcon.con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
throw;
}
And this is an image of the error: