Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send data that i have in the list view into my database. my list view has 5 columns and my database has 5 fields. the way i wrote the insert - it inserts an entire row with all five column information into one field in the database. I could i parse through the string to properly insert every column with its field. here is my Code:

private void btnSave_Click(object sender, EventArgs e)
        {
            //save listview to database
           
            conn.Open();
           for (int cnt = 0; cnt <= listView1.Items.Count; cnt++)
            {
                string query = "insert into Log values('" + listView1.Items[cnt].Text +"')";
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                
            }
            conn.Close();
Posted

nouraleyoon wrote:
string query = "insert into Log values('" + listView1.Items[cnt].Text +"')";


This does exactly what you asked. IF the user was able to enter the text, then they can also erase your DB because this is badly written. You should use parameterised queries and make it so that it pulls out the values you need, instead of passing them all as one value.

The fact that it's called listview1 makes me think you're just writing code for fun, and that's cool. read up on SQL injection, if you ever want to write production quality code.

The other thing is, look at the SQL you generate, then think about what it does, and how it needs to change to do what you want. Specifying the columns you want to insert into is one way to make this either crash or do what you want ( instead of 'working' but not doing what you hoped it would )
 
Share this answer
 
I am new to the sql statements and database connectivity so i might seem a little slow gathering my thoughts. Is it possible that i could insert depending on the listview positions. Because i am having trouble in seeing how could i break down the forloop to read within columns. Or within the forloop i would assign what i found to a string and then parse through that string and entering data that way but at the same time i am not clear to how to go about parsing the string. Clarifications and examples are very useful to me.
 
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