I have created a Winforms application using SQL Server for the database. The problem is that, after the code executes successfully, the data is not being inserted into the table. I have verified that the connection string is correct and the code appears to be correct as well, but the data still isn't being inserted. Can anyone help me with this?
Here is the code I am using:
private void StoreData(){ try { // Log connection string details (mask sensitive information if necessary) LogInformation($"Attempting to open connection with connection string: {connectionString}"); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Log which database and server the connection is using LogInformation($"Connected to database: {connection.Database} on server: {connection.DataSource} with connection string: {connection.ConnectionString}"); string query = "INSERT INTO Production_Master (PlanId, ModelId, PartId, Part_Sr_No, SerialId, [User], Date_Time) " +"VALUES (@PlanId, @ModelId, @PartId, @Part_Sr_No, @SerialId, @User, @Date_Time)"; using (SqlCommand command = new SqlCommand(query, connection)) { // Log which table is being accessed LogInformation("Accessing table: Production_Master"); // Define constant values outside the loop command.Parameters.Add("@PlanId", SqlDbType.Int).Value = _PlanId; command.Parameters.Add("@ModelId", SqlDbType.Int).Value = _MId; command.Parameters.Add("@SerialId", SqlDbType.NVarChar, 50).Value = P_T_QR_DATA; command.Parameters.Add("@User", SqlDbType.Int).Value = 0; // Assuming this is a constant value for (int i = 0; i < partId.Count; i++) { // Remove previously added parameters command.Parameters.Remove("@PartId"); command.Parameters.Remove("@Part_Sr_No"); command.Parameters.Remove("@Date_Time"); // Set parameters that change per iteration command.Parameters.Add("@PartId", SqlDbType.Int).Value = partId[i]; command.Parameters.Add("@Part_Sr_No", SqlDbType.NVarChar, 50).Value = Station3Part[i]; command.Parameters.Add("@Date_Time", SqlDbType.DateTime).Value = DateTime.Now; // Log the parameters being inserted LogInformation($"Inserting data: PlanId={_PlanId}, ModelId={_MId}, PartId={partId[i]}, Part_Sr_No={Station3Part[i]}, SerialId={P_T_QR_DATA}, User=0, Date_Time={DateTime.Now}"); int rowsAffected = command.ExecuteNonQuery(); if (rowsAffected > 0) { richTextBox1.AppendText($"Data for Part {partId[i]} stored successfully.\n"); // Log success message LogInformation($"Data for PartId={partId[i]} stored successfully."); } else { // Handle potential insertion failure for each iteration // Log an error message or display a specific message for the failed part LogError($"Failed to store data for PartId={partId[i]}."); } } } } // Clear variables after storing data tankSR = ""; Station3Part.Clear(); station3Tank = ""; // Log clearing of variables LogInformation("Cleared variables after storing data."); } catch (SqlException ex) { LogException(ex); }}
Here is the log from text file...
2024-05-24 15:37:27 [INFO] Connected to database: CKDDB on server: 10.12.101.1 with connection string: Data Source=10.12.101.1;User ID=sa;Initial Catalog=CKDDB;Integrated Security=False;2024-05-24 15:37:27 [INFO] Accessing table: Production_Master2024-05-24 15:37:27 [INFO] Cleared variables after storing data.2024-05-24 15:41:14 [INFO] Attempting to open connection with connection string: Data Source=10.12.101.1;User ID=sa;Password=Bal@1234;Initial Catalog=CKDDB;Integrated Security=False;