Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

as title said

how to add selected multiple items in listbox to database every selected item to new record

i have listbox that bounded from database and i want to add every selected item to new record in sql server DB iam using C#

more explain i want every item that i select to insert to new row

this is my code but this insert the whole items in listbox

for (int indexCounter = 0; indexCounter < test_names.Items.Count; indexCounter++)
        {
            
           

            string patient_id = Request.QueryString["id"];
            SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("INSERT INTO [tetstttt] (test_id,date,pname) VALUES ('" + test_names.Items[indexCounter] + "',N'" + 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);
        }


sorry for bad English

thanks in advanced
Posted
Updated 2-Feb-14 18:54pm
v3
Comments
Rockstar_ 3-Feb-14 0:27am    
What have you tried?
Rockstar_ 3-Feb-14 0:28am    
Google.......
seems you need to insert data to database.
JoCodes 3-Feb-14 1:21am    
IS test_id primary key for the table?Then any reason you are inserting test_id from the code? And specify whats the issue you are facing with the above code

Hope this will meet your requirement

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 [tetstttt] (test_id,date,pname) 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);
              
} }</pre>
 
Share this answer
 
Try this


C#
for (int indexCounter = 0; indexCounter < test_names.Items.Count; indexCounter++)
       {
           if (test_names.Items[indexCounter].Selected)
           {

               string patient_id = Request.QueryString["id"];
               SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
               SqlCommand cmd = new SqlCommand("INSERT INTO [tetstttt] (test_id,date,pname) VALUES ('" + test_names.Items[indexCounter] + "',N'" + 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);
           }
       }
 
Share this answer
 
Comments
Praveen_P 3-Feb-14 1:36am    
gud , 5 !
Karthik_Mahalingam 3-Feb-14 1:40am    
:)
Siva Hyderabad 3-Feb-14 4:58am    
Hai karthik,busy na?
Karthik_Mahalingam 3-Feb-14 5:26am    
tell me
Siva Hyderabad 3-Feb-14 6:01am    
I have an exception..It's comes no:of times...
hello

another thing 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,data_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
 
Share this answer
 
Comments
CHill60 4-Feb-14 16:40pm    
You have posted a comment as a solution. If you had done this before anyone had answered your question then it would have dropped out of the list of posts awaiting solutions and fewer people would have viewed it. As it is, this is really another question so you could have posted a new question, or used the Improve Question Link
In answer to the subsequent question regarding error
Quote:
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.
generated by the line
SQL
SqlCommand cmd = new SqlCommand("INSERT INTO [patient_test_data] (test_id,test_date,pat_id,data_id) VALUES ('" + selectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);

[Details given here as subsequent question posted as a solution and may disappear]

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/[^]
 
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