Batch add data to the DataGrid of ASP. NET

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.