Zahraa
Zahraa

Reputation: 67

Inserting values from drop down list and text box to the database

Why this code is not work I try to insert student ID and student name from text box table and Major, Course from drop down list

int sID = Convert.ToInt32(TextBox1.Text);
string Name = TextBox2.Text;
var mjr = DropDownList1.SelectedItem.Value;
var crs = DropDownList2.SelectedItem.Text;

string registered = "insert into Registered (Student_name, StudentID, Major, Course) values(@sName ,@SID, @major , @course)";

while (reader.Read())
{
    ints.Add(reader.GetInt32(0));

    if (a.Count() <= ints.Count() && !a.Except(ints).Any())
    {
        SqlCommand com4 = new SqlCommand(registered, con);

        com4.Parameters.AddWithValue("sName", Name);
        com4.Parameters.AddWithValue("SID", sID);
        com4.Parameters.AddWithValue("major", mjr);
        com4.Parameters.AddWithValue("course", crs);
        com4.ExecuteNonQuery();

Upvotes: 1

Views: 263

Answers (1)

Dheeraj Kumar
Dheeraj Kumar

Reputation: 4175

You need to add "@" with scalar attributes names

 com4.Parameters.AddWithValue("@sName", Name);
 com4.Parameters.AddWithValue("@SID", sID);
 com4.Parameters.AddWithValue("@major", mjr);
 com4.Parameters.AddWithValue("@course", crs);
 com4.ExecuteNonQuery();

Upvotes: 1

Related Questions