I am having this problem when I'm trying to insert data into a database with a button and textboxes. Everything works fine except a foreign key that is inside the table throws this error:
Must declare the scalar variable "@IDprodajalne"
I am getting kind of desperate so I really need your help now.
if (textBoxImeProdajalca.Text != "" && textBoxPriimekProdajalca.Text != "")
{
try
{
string query = "insert into Prodajalec values (@IDProadajalne, @ImeProdajalca, @PriimekProdajalca)";
using (connection = new SqlConnection(connectionString)
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@IDProdajalne", ComboBoxIDprodajalne.SelectedItem.ToString());
command.Parameters.AddWithValue("@ImeProdajalca", textBoxImeProdajalca.Text);
command.Parameters.AddWithValue("@PriimekProdajalca", textBoxPriimekProdajalca.Text);
command.ExecuteNonQuery();
}
MessageBox.Show("Uspešno dodano");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Vnesite vse podatke");
}
And the string declarations
SqlConnection connection;
string connectionString;
public Form1()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["Naloga.Properties.Settings.ProdajaConnectionString"].ConnectionString;
}
Copy "IDprodajalne" from the error. Press Ctrl+F to search your question for it. It appears twice, both in this line:
command.Parameters.AddWithValue("@IDProdajalne", ComboBoxIDprodajalne.SelectedItem.ToString());
It does not appear in this line:
string query = "insert into Prodajalec values (@IDProadajalne,@ImeProdajalca,@PriimekProdajalca)";
"IDPro a dajalne" is a different string from "IDProdajalne". It has another "a" in it.
Fix the spelling of the one in query
:
string query = "insert into Prodajalec values (@IDProdajalne,@ImeProdajalca,@PriimekProdajalca)";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.