Click here to Skip to main content
15,907,000 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In gridview as follows


Room Course Room Course


11 AFF 21 MFA

12 REO 22

13 RM 23 EFA

14 24 ERS


Save code as follows


foreach (GridViewRow row in gvClassRooms.Rows)
{
Sql = "insert into TB_Room_Allocation_SMS (RoomNo,Minor_Code) " +
"values( '" + row.Cells[0].Text + "','" + row.Cells[1].Text + "')";
scon.ExecSql(Sql);

Sql = "insert into TB_Room_Allocation_SMS (RoomNo,Minor_Code) " +
"values( '" + row.Cells[2].Text + "','" + row.Cells[3].Text + "')";
scon.ExecSql(Sql);

Label4.Text = "Records Inserted Successfully";
scon.Con.Close();
}


In database records as follows



in databse null values are also saving. for Room 14 and 22 course is not there.

so that two rooms cannot be inserted in the database.
Room Course

11 AFF
12 REO
13 RM
14  
21 MFA
22  
23 EFA
24 ERS


how can i do in asp.net using csharp.


Rgds,
Narasiman P.
Posted

check for the value in Cell1
If it is not empty, then only insert into DB.
similar for other insert statement also.
 
Share this answer
 
v2
pls try this

C#
foreach (GridViewRow row in gvClassRooms.Rows)
            {
                if (!string.IsNullOrEmpty(row.Cells[1].Text))
                {
                     Sql = "insert into TB_Room_Allocation_SMS (RoomNo,Minor_Code) " +
                    "values( '" + row.Cells[0].Text + "','" + row.Cells[1].Text + "')";
                    scon.ExecSql(Sql);
                }

                if (!string.IsNullOrEmpty(row.Cells[3].Text))
                {
                    Sql = "insert into TB_Room_Allocation_SMS (RoomNo,Minor_Code) " +
                   "values( '" + row.Cells[2].Text + "','" + row.Cells[3].Text + "')";
                    scon.ExecSql(Sql);
                }

                Label4.Text = "Records Inserted Successfully";
                scon.Con.Close();
            }


Hope this will help...
 
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