Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello

iam trying to add more thank one filed but i get error

C#
foreach (ListItem lst in test_names.Items)
        {
            if (lst.Selected)
            {
                string selectedValue = lst.Value;
                string patient_id = Request.QueryString["id"];
                SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
                SqlCommand cmd = new SqlCommand("INSERT INTO [patient_test_data] (test_id,test_date,pat_id) VALUES ('" + selectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);
                cmd.Connection = cnm;
                cmd.CommandType = CommandType.Text;
                cnm.Open();
                cmd.ExecuteScalar();
                cnm.Close();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "show_error", "showSuccess('Test Has Been Added Successfully',2000);", true);
 
            }
        }



this is the error that i got

There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.


i don't know how to insert with this function



OMG is it me :@ let me kill my self OMG !!!! how the hell i didn't see this -_- thanks BTW so let us go to the next step
now i want to create a daynamic control 3 label and 3 textbox depended on value comes from sql data reader "number of columns"


this is my code
<pre lang="c#">cn = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
            cmd = new SqlCommand("SELECT COUNT( pat_id) AS patient_test FROM patient_test_data WHERE dbo.patient_test_data.test_date >= cast(getdate() as Date) AND dbo.patient_test_data.pat_id = 4;", cn);
            cmd.CommandType = CommandType.Text;
            cmd.Connection.Open();
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            while (dr.Read())
            {
                int number_test = int.Parse(dr["patient_test"].ToString());
                for (int number = int.Parse(dr["patient_test"].ToString()); number <= number_test; number++)
                {
                    TextBox txt = new TextBox();
                    //txt.Text = dr["Col1"].ToString();
                    TextBox txt2 = new TextBox();
                    //txt2.Text = dr["Col2"].ToString();
                    TextBox txt3 = new TextBox();
                    //txt3.Text = dr["Col3"].ToString();
                    this.Controls.Add(txt);
                    this.Controls.Add(txt2);
                    this.Controls.Add(txt3);
                }
            }


i just i don't know i got this error Collection was modified; enumeration operation may not execute.

thanks in advanced
Posted
Updated 4-Feb-14 14:14pm
v2
Comments
CHill60 5-Feb-14 14:29pm    
You're lucky I spotted your next question - you should really have posted that as a new question instead of adding it onto the end of a post that already has a solution - more people will see it.
Well - apart from a for() loop that will only execute once (and assuming that you will position/size the controls later) there is nothing wrong with your code. I don't see any enumeration in that code block either - are you sure that's where the exception was thrown from?

1 solution

Your sql command INSERT INTO [patient_test_data] (test_id,test_date,pat_id,data_id) is expecting 4 values to be supplied. You have only supplied 3 of them...

test_id <= selectedValue
test_date <= Now
pat_id <= patient_id

data_id <= ????????????????


As an aside, when you are creating SQL commands be wary of the risk of SQL Injection Attacks - see this reference http://bobby-tables.com/[^]

[Copied from my solution at how to add selected items in listbox to database every selected item to new record[^]]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900