Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm still newbie in C# programming. I need some help on my project.

I have datagridview that i fill it with couple data from different table in my database, the column in my datagridview is Choose(checkbox column), Item(textbox column) and Detail(textbox column).
what i want to do is, when user checked the checkbox in a row or more in Choose column it will saved the checked row as new record in my new database.

here little part of my code to execute that,
C#
DataTable dtChanged = ((DataTable)dgvItem.DataSource).GetChanges();
if (dtChanged == null)
    return;

SqlConnection sqlConn = new SqlConnection(GlobalVar.gstrConnStr);
sqlConn.Open();

SqlCommand sqlCmd = new SqlCommand();

foreach (DataRow drChanged in dtChanged.Rows)
{
    if (!(Boolean)drChanged["Choosen"])
        continue;

    if (drChanged["Choosen"].ToString() == "True")
    {
        string strSeq = drChanged["Choosen"].ToString();
        string strItemID = drChanged["Items"].ToString().Replace("'", "''");

        string strSql = string.Format("INSERT INTO mkar1(meID,meSeq,meItemID,meUser,meDate)" +
            "VALUES ('{0}',{1},'{2}','{3}','{4:yyyy/MM/dd HH:mm:ss}')",
            txtNIKmkar.Text.Trim().Replace("'", "''"), strSeq, strItemID, GlobalVar.gstrConnStr, DateTime.Now);

        sqlCmd.CommandText = strSql;
        sqlCmd.Connection = sqlConn;
        sqlCmd.ExecuteNonQuery();
    }
}
sqlCmd.Dispose();
sqlConn.Close();
sqlConn.Dispose();

i know the code i write is still have problem and i need some help to fix it because i dont know how to do it.

and the database i will going to use,
meID (PK, nchar 15, not null)
meSeq (PK, tinyint, not null)
meItemID (nchar 10, not null)
meUser (nchar 20, not null)
meDate (datetime, not null)

And here is my question,
1. how to save into the database?
2. as for the column "meSeq" i have to save numeric number start from 0 for every checked row. how can i save every checked row as new record?

what i need to do?

please help, i will greatly appreciate every answer.
Posted
Comments
piyush_singh 24-Sep-13 5:45am    
First check in the first line, are you getting any rows for the table, dtChanged. Because if there are no rows then, your foreach loop will not be executed.

Secondly, what is the error, that you are getting when running this code.

Thirdly, inside the foreach loop, you're twice checking the value of drChanged["Choosen"], which is unnecessary.

1. To save to database you should write something like :

C#
using (SqlConnection con = new SqlConnection(connectionString))
{
           
    //
    // Open the SqlConnection.
    //
    con.Open();

    string insertString = @"insert into yourdatabase (meID,meSeq, meItemID,meUser, meDate) values ('id','seq','itemId','user','date')";
    SqlCommand cmd = new SqlCommand(insertString, connectionString);
    cmd.ExecuteNonQuery();

    con.Close();

}


2. First of all, field MeSeq is probably a FK. In this case your can save every checked row for a user or add new.
 
Share this answer
 
v3
thanks to piyush_singh and Alexander Dymshyts for being willing to provide answer.
i've solved it, actually it just need new variable for increment each checked checkbox to be added as new record.

C#
int intSeq = -1;
foreach (DataRow drChanged in dtChanged.Rows)
            {
                if (!(Boolean)drChanged["Choosen"])
                    continue;
 
                if ((Boolean)drChanged["Choosen"])
                {
                    intSeq++;
                    string strItemID = drChanged["Items"].ToString().Replace("'", "''");
 
                    string strSql = string.Format("INSERT INTO  mkar1(meID,meSeq,meItemID,meUser,meDate)" +
                        "VALUES ('{0}',{1},'{2}','{3}','{4:yyyy/MM/dd HH:mm:ss}')",
                        txtNIKmkar.Text.Trim().Replace("'", "''"), intSeq, strItemID, GlobalVar.gstrUsrStr, DateTime.Now);
 
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