I want to insert a string list of device name and related seq number into a table on SQL server 2014. The IDE is Visual Studio 2015, the programming language is C#.
When I run the program and click the button14, the error is:
The parameterized query '(@ID int,@NAME nvarchar(4000),@RSSI int)INSERT BeaconInfo (ID, N' expects the parameter '@NAME', which was not supplied.
I am not sure my codes of inserting sql is correct or not.
public partial class Form1 : Form
{
int seqnumber = 333;
List<string> items;
string tmp_name;
BluetoothDeviceInfo[] devices;
public Form1()
{
items = new List<string>();
InitializeComponent();
}
private void startScan()
{
listBox1.DataSource = null;
listBox1.Items.Clear();
items.Clear();
Thread bluetoothScanThread = new Thread(new ThreadStart(scan));
bluetoothScanThread.Start();
}
private void scan()
{
updateUI("Starting Scan..");
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevicesInRange();
updateUI("Scan complete");
updateUI(devices.Length.ToString() + " devices discovered");
foreach (BluetoothDeviceInfo d in devices)
{
items.Add(d.DeviceName);
}
updateDeviceList();
}
private void button14_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("....");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT BeaconInfo (ID, Name, RSSI) VALUES (@ID, @NAME, @RSSI)";
cmd.Parameters.AddWithValue("@ID", seqnumber);
cmd.Parameters.AddWithValue("@NAME", tmp_name);
cmd.Parameters.AddWithValue("@RSSI", 55);
cmd.Connection = sqlConnection1;
for (int j = 0; j < items.Count; j++)
{
seqnumber = seqnumber + 1;
tmp_name = items[j];
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
}