Click here to Skip to main content
15,889,527 members
Home / Discussions / C#
   

C#

 
AnswerRe: Convert code C# 2008 to code C# 2005 ? Pin
OriginalGriff24-Feb-15 22:05
mveOriginalGriff24-Feb-15 22:05 
GeneralRe: Convert code C# 2008 to code C# 2005 ? Pin
Member 245846725-Feb-15 16:22
Member 245846725-Feb-15 16:22 
GeneralRe: Convert code C# 2008 to code C# 2005 ? Pin
OriginalGriff25-Feb-15 21:51
mveOriginalGriff25-Feb-15 21:51 
GeneralRe: Convert code C# 2008 to code C# 2005 ? Pin
Member 24584673-Mar-15 15:42
Member 24584673-Mar-15 15:42 
QuestionInsert the data of datagridview in a database. Pin
Ibrahim.elh24-Feb-15 20:45
Ibrahim.elh24-Feb-15 20:45 
QuestionRe: Insert the data of datagridview in a database. Pin
Eddy Vluggen25-Feb-15 0:31
professionalEddy Vluggen25-Feb-15 0:31 
AnswerRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 1:55
Ibrahim.elh25-Feb-15 1:55 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 2:09
mveRichard Deeming25-Feb-15 2:09 
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

The ArgumentOutOfRangeException suggests that your rows don't contain 12 cells. (You're ignoring the first cell.) Did you actually mean to take the values from 11 cells, starting at index 0?

You're creating a new OdbcDataAdapter for each row in the grid, and then throwing away all but the last one.

You're specifying an INSERT command as the SelectCommand property of the data adapter.

Assuming you just want to insert the rows into the table, try something like this:
C#
using (var connection = new OdbcConnection(lc.Connexion_locale))
using (var command = new OdbcCommand("insert into thisdet2 values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", connection))
{
    // ODBC doesn't use named parameters, so the names don't matter:
    var p0 = command.Parameters.Add("@p0", OdbcType.NVarChar);
    var p1 = command.Parameters.Add("@p1", OdbcType.NVarChar);
    var p2 = command.Parameters.Add("@p2", OdbcType.NVarChar);
    var p3 = command.Parameters.Add("@p3", OdbcType.NVarChar);
    var p4 = command.Parameters.Add("@p4", OdbcType.NVarChar);
    var p5 = command.Parameters.Add("@p5", OdbcType.NVarChar);
    var p6 = command.Parameters.Add("@p6", OdbcType.NVarChar);
    var p7 = command.Parameters.Add("@p7", OdbcType.NVarChar);
    var p8 = command.Parameters.Add("@p8", OdbcType.NVarChar);
    var p9 = command.Parameters.Add("@p9", OdbcType.NVarChar);
    var p10 = command.Parameters.Add("@p10", OdbcType.NVarChar);
    var p11 = command.Parameters.Add("@p11", OdbcType.NVarChar);
    
    connection.Open();
    
    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
    {
        var row = dataGridView1.Rows[i];
        
        p0.Value = row.Cells[0].Value;
        p1.Value = row.Cells[1].Value;
        p2.Value = row.Cells[2].Value;
        p3.Value = row.Cells[3].Value;
        p4.Value = row.Cells[4].Value;
        p5.Value = row.Cells[5].Value;
        p6.Value = row.Cells[6].Value;
        p7.Value = row.Cells[7].Value;
        p8.Value = row.Cells[8].Value;
        p9.Value = row.Cells[9].Value;
        p10.Value = row.Cells[10].Value;
        p11.Value = row.Cells[11].Value;
        
        command.ExecuteNonQuery();
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer



modified 25-Feb-15 10:54am.

GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 2:58
Ibrahim.elh25-Feb-15 2:58 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 3:02
mveRichard Deeming25-Feb-15 3:02 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 3:06
Ibrahim.elh25-Feb-15 3:06 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 3:10
mveRichard Deeming25-Feb-15 3:10 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 3:12
Ibrahim.elh25-Feb-15 3:12 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 3:14
mveRichard Deeming25-Feb-15 3:14 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 3:39
Ibrahim.elh25-Feb-15 3:39 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 3:49
mveRichard Deeming25-Feb-15 3:49 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 3:59
Ibrahim.elh25-Feb-15 3:59 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 4:08
mveRichard Deeming25-Feb-15 4:08 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 4:11
Ibrahim.elh25-Feb-15 4:11 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 4:25
mveRichard Deeming25-Feb-15 4:25 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 4:31
Ibrahim.elh25-Feb-15 4:31 
GeneralRe: Insert the data of datagridview in a database. Pin
Richard Deeming25-Feb-15 4:41
mveRichard Deeming25-Feb-15 4:41 
GeneralRe: Insert the data of datagridview in a database. Pin
Ibrahim.elh25-Feb-15 4:47
Ibrahim.elh25-Feb-15 4:47 
GeneralRe: Insert the data of datagridview in a database(error). Pin
Ibrahim.elh25-Feb-15 4:48
Ibrahim.elh25-Feb-15 4:48 
GeneralRe: Insert the data of datagridview in a database(error). Pin
Richard Deeming25-Feb-15 4:54
mveRichard Deeming25-Feb-15 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.