Batch add data to the DataGrid of ASP. NET
In a previous school project, the student management system was applied
Add data in batches in the DataGrid of ASP. NET, and add data cyclically in the DataGrid
For (I = 0; I <(INT) This. mydatagrid. Items. Count; I ++)
{
// Process
}
Browse data
Add data
Database Table Data
Student table
Stu_information
[ID] [nvarchar]
[Name] [nvarchar]
[Sex] [nvarchar]
[Birth] [nvarchar]
[Age] [nvarchar]
[Department] [nvarchar]
[Class] [nvarchar]
[Post] [nvarchar]
[Political] [nvarchar]
[Address] [nvarchar]
[Email] [nvarchar]
[Phone] [nvarchar]
[Telephone1] [nvarchar]
[Telephone2] [nvarchar]
[Information] [nvarchar]
Attendance table
Kaoqin
[Kaoqinid] [int] identity (1, 1) not null,
[Datetime] [datetime] Null,
[ID] [char]
[Title] [char]
[Week] [char]
[Dayno] [char]
[Type] [char]
[Times] [int] Null,
[Information] [char]
Page aspx File
<Asp: DataGrid id = "mydatagrid" autogeneratecolumns = "false" width = "100%" headerstyle-horizontalalign = "center"
Itemstyle-horizontalalign = "center" runat = "server">
<Columns>
<Asp: boundcolumn datafield = "ID" sortexpression = "ID" headerstyle-width = "10%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff" headertext = "student ID"/>
<Asp: boundcolumn datafield = "name" sortexpression = "name" headerstyle-width = "8%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff" headertext = "name"/>
<Asp: boundcolumn datafield = "sex" sortexpression = "sex" headerstyle-width = "5%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff" headertext = "gender"/>
<Asp: templatecolumn headertext = "late (times)" headerstyle-width = "15%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff">
<Itemtemplate>
<Asp: textbox id = "late" width = "30px" runat = "server"/>
</Itemtemplate>
</ASP: templatecolumn>
<Asp: templatecolumn headertext = "(times)" headerstyle-width = "15%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff">
<Itemtemplate>
<Asp: textbox id = "leaveearly" width = "30px" runat = "server"/>
</Itemtemplate>
</ASP: templatecolumn>
<Asp: templatecolumn headertext = "absenteeism (times)" headerstyle-width = "15%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff">
<Itemtemplate>
<Asp: textbox id = "absenteeism" width = "30px" runat = "server"/>
</Itemtemplate>
</ASP: templatecolumn>
<Asp: templatecolumn headertext = "Leave (times)" headerstyle-width = "15%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff">
<Itemtemplate>
<Asp: textbox id = "leave" width = "30px" runat = "server"/>
</Itemtemplate>
</ASP: templatecolumn>
<Asp: templatecolumn headertext = "Remarks" headerstyle-width = "40%" headerstyle-cssclass = "topbar"
Headerstyle-forecolor = "# ffffff">
<Itemtemplate>
<Asp: textbox id = "information" width = "200px" runat = "server"/>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>
Backend Aspx. CS
Protected void btn_add (Object sender, imageclickeventargs E)
{
String link1 = configurationsettings. deleettings ["link_local"]. tostring ();
Sqlconnection conn1 = new sqlconnection (link1 );
String sqlcmd = "";
Int I;
Bool flag = false;
Conn1.open ();
For (I = 0; I <(INT) This. mydatagrid. Items. Count; I ++)
{
String typename = "";
If (textbox) mydatagrid. items [I]. cells [3]. controls [1]). Text. Trim ()! = "")
{
Typename = "late ";
Flag = true;
Sqlcmd = "insert into kaoqin (ID, week, datetime, type, times, Information) values (@ ID, @ week, @ datetime, @ type, @ times, @ Information) ";
Sqlcommand comm = new sqlcommand (sqlcmd, conn1 );
Comm. Parameters. Add ("@ ID", sqldbtype. Char, 20 );
Comm. Parameters. Add ("@ Week", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ datetime", sqldbtype. datetime, 8 );
Comm. Parameters. Add ("@ Type", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ Times", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ information", sqldbtype. Char, 100 );
Comm. Parameters ["@ ID"]. value = This. mydatagrid. items [I]. cells [0]. Text. Trim ();
Comm. Parameters ["@ Week"]. value = week. selecteditem. value;
Comm. Parameters ["@ datetime"]. value = datetime. Now. tostring ("mm DD, YYYY ");
Comm. Parameters ["@ Type"]. value = typename. Trim ();
Comm. Parameters ["@ times"]. value = (textbox) mydatagrid. items [I]. cells [3]. controls [1]). Text. Trim ();
Comm. parameters ["@ information"]. value = (textbox) This. mydatagrid. items [I]. cells [7]. controls [1]). text. trim ();
Comm. executenonquery ();
}
If (textbox) mydatagrid. items [I]. cells [4]. controls [1]). Text. Trim ()! = "")
{
Typename = "leave early ";
Flag = true;
Sqlcmd = "insert into kaoqin (ID, week, datetime, type, times, Information) values (@ ID, @ week, @ datetime, @ type, @ times, @ Information) ";
Sqlcommand comm = new sqlcommand (sqlcmd, conn1 );
Comm. Parameters. Add ("@ ID", sqldbtype. Char, 20 );
Comm. Parameters. Add ("@ Week", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ datetime", sqldbtype. datetime, 8 );
Comm. Parameters. Add ("@ Type", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ Times", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ information", sqldbtype. Char, 100 );
Comm. Parameters ["@ ID"]. value = This. mydatagrid. items [I]. cells [0]. Text. Trim ();
Comm. Parameters ["@ Week"]. value = week. selecteditem. value;
Comm. Parameters ["@ datetime"]. value = datetime. Now. tostring ("mm DD, YYYY ");
Comm. Parameters ["@ Type"]. value = typename. Trim ();
Comm. Parameters ["@ times"]. value = (textbox) mydatagrid. items [I]. cells [4]. controls [1]). Text. Trim ();
Comm. parameters ["@ information"]. value = (textbox) This. mydatagrid. items [I]. cells [7]. controls [1]). text. trim ();
Comm. executenonquery ();
}
If (textbox) mydatagrid. items [I]. cells [5]. controls [1]). Text. Trim ()! = "")
{
Typename = "absenteeism ";
Flag = true;
Sqlcmd = "insert into kaoqin (ID, week, datetime, type, times, Information) values (@ ID, @ week, @ datetime, @ type, @ times, @ Information) ";
Sqlcommand comm = new sqlcommand (sqlcmd, conn1 );
Comm. Parameters. Add ("@ ID", sqldbtype. Char, 20 );
Comm. Parameters. Add ("@ Week", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ datetime", sqldbtype. datetime, 8 );
Comm. Parameters. Add ("@ Type", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ Times", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ information", sqldbtype. Char, 100 );
Comm. Parameters ["@ ID"]. value = This. mydatagrid. items [I]. cells [0]. Text. Trim ();
Comm. Parameters ["@ Week"]. value = week. selecteditem. value;
Comm. Parameters ["@ datetime"]. value = datetime. Now. tostring ("mm DD, YYYY ");
Comm. Parameters ["@ Type"]. value = typename. Trim ();
Comm. Parameters ["@ times"]. value = (textbox) mydatagrid. items [I]. cells [5]. controls [1]). Text. Trim ();
Comm. parameters ["@ information"]. value = (textbox) This. mydatagrid. items [I]. cells [7]. controls [1]). text. trim ();
Comm. executenonquery ();
}
If (textbox) mydatagrid. items [I]. cells [6]. controls [1]). Text. Trim ()! = "")
{
Typename = "leave ";
Flag = true;
Sqlcmd = "insert into kaoqin (ID, week, datetime, type, times, Information) values (@ ID, @ week, @ datetime, @ type, @ times, @ Information) ";
Sqlcommand comm = new sqlcommand (sqlcmd, conn1 );
Comm. Parameters. Add ("@ ID", sqldbtype. Char, 20 );
Comm. Parameters. Add ("@ Week", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ datetime", sqldbtype. datetime, 8 );
Comm. Parameters. Add ("@ Type", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ Times", sqldbtype. Char, 10 );
Comm. Parameters. Add ("@ information", sqldbtype. Char, 100 );
Comm. Parameters ["@ ID"]. value = This. mydatagrid. items [I]. cells [0]. Text. Trim ();
Comm. Parameters ["@ Week"]. value = week. selecteditem. value;
Comm. Parameters ["@ datetime"]. value = datetime. Now. tostring ("mm DD, YYYY ");
Comm. Parameters ["@ Type"]. value = typename. Trim ();
Comm. Parameters ["@ times"]. value = (textbox) mydatagrid. items [I]. cells [6]. controls [1]). Text. Trim ();
Comm. parameters ["@ information"]. value = (textbox) This. mydatagrid. items [I]. cells [7]. controls [1]). text. trim ();
Comm. executenonquery ();
}
}
Conn1.close ();
If (flag = true)
{
Lblmessage. Text = "added successfully! ";
}
Else
Lblmessage. Text = "No information added ";
}
Source code download