Dinesh udayanga
Dinesh udayanga

Reputation: 23

The database that is Connected for my Visual Studio program, does not take the data into the tables when run the program

Want to say, this is the first time that I am working with SQL, Visual Studio, C#. In visual studio interface, there is a button called inventory and when click it after connecting the interface. Then what I need to do is put those details into a table in a database connected to the visual studio program.

I made the database including necessary tables and connected it with Visual Studio. Program is built successfully and also run. But the data is not inserted into the tables that I have created in the database. There is no any error is shown. We only added some amendments to the original visual studio program (.cs) to insert the details of tags into our database and a few more operations with those data and database. But now, we can see that the database is performing those operations.

After running the program, tables do not show any data that it was supposed to insert from the.CS program. Need to know what is the reason and how can we see those details in the database.

I expect to see data filling into tables.

This is my code:

con.Open();
commandInsert = new SqlCommand(
                       "INSERT INTO table3(EPC,PC,Identificationcount,RSSI,CarrierFrequency) VALUES (@EPC,@PC,@Identificationcount,@RSSI,@CarrierFrequency)",
                       con
                    );
commandInsert.Parameters.Add("@EPC", row[2].ToString());
commandInsert.Parameters.Add("@PC", row[0].ToString());
commandInsert.Parameters.Add("@Identificationcount", row[5].ToString());
commandInsert.Parameters.Add("@RSSI", (Convert.ToInt32(row[4]) - 129).ToString() + "dBm");
commandInsert.Parameters.Add("@CarrierFrequency", row[6].ToString());
commandInsert.ExecuteNonQuery();

Upvotes: 1

Views: 92

Answers (1)

LoaStaub
LoaStaub

Reputation: 63

Try to make a Class.

using System.Data.SqlClient;

class MyDataBase
{
    public SqlConnection MyConnection = new SqlConnection(ADD CONNECTION STRING HERE);
    public SqlCommand MyCommand = MyConnection.CreateCommand();
}

Let's say you have a Table "testTable" with 3 Columns. 1 int called "id" (if you dont have Auto Incrementing Primary Key), 1 text/varchar "description" and 1 datetime "createDate".

Now do this in your Main Class:

private void InsertIntoTableXYZ(int valueID, string valueDescr, DateTime valueDate)
{
    MyDataBase.MyCommand.CommandText = 
              "INSERT INTO testTable (id,description,createDate) " +
              "VALUES (" + valueID + ",'" + valueDescr + "','" + valueDate "')";
                   // no '' because it is int
    MyDataBase.MyConnection.Open();
    MyDataBase.MyCommand.ExecuteNonQuery();
    MyDataBase.MyConnection.Close();
}

And then you call the method somewhere in your Main Code

InsertIntoTableXYZ(1, "hello", DateTime.Now)

Upvotes: 1

Related Questions