hello,
i have done it using the datalist and with MySQl database.
You can add multiple no of controls on button click and enetring the value in text box..
<%@ Page Title="" Language="C#" MasterPageFile="~/AssetMaster.master" AutoEventWireup="true"
CodeFile="CreateModifyMultipleAssets.aspx.cs" Inherits="Default2" ValidateRequest="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
.style19
{
}
.style20
{
height: 30px;
}
.style22
{
width: 181px;
}
.style23
{
height: 30px;
width: 181px;
}
#tb
{
height: 81px;
}
.style24
{
height: 30px;
width: 160px;
}
.ModalBackground
{
background-color: Gray;
filter: alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
</style>
<link href="App_Themes/ThemeAdmin/MainModalDialog.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<div id="content_div">
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr>
<td align="left" style="padding-left: 30px;" id="id_td_pageHeading" colspan="2">
<asp:Label ID="lblName" runat="server" Text="Create/Modify Asset" Font-Bold="True"
Font-Size="Medium" CssClass="cslTitle"></asp:Label>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style24">
<asp:Label ID="Label1" runat="server" Text="Add More Rows" Font-Bold="true"></asp:Label>
</td>
<td align="left" class="style22">
<asp:TextBox ID="txtAdd" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Enter number in Add textbox"
ValidationGroup="addmore" ControlToValidate="txtAdd">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtAdd"
ErrorMessage="Enter only numeric value in Text box" ValidationGroup="addmore"
ValidationExpression="[0-9]*">*</asp:RegularExpressionValidator>
</td>
<td align="left">
<asp:Button ID="btnAdd" runat="server" Text="ADD" OnClick="btnAddControl_Click" CssClass="asset-viewer-btn"
ValidationGroup="addmore" />
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style24">
<asp:ValidationSummary ID="ValidationSummary3" ShowMessageBox="true" ShowSummary="false"
ValidationGroup="addmore" runat="server" />
</td>
<td align="left" class="style23">
</td>
<td align="left" class="style20">
<asp:Button ID="btnSaveAllRecords" runat="server" Text="Save All Records" OnClick="btnSaveAllRecords_Click"
CssClass="asset-viewer-btn" />
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style24">
</td>
<td align="left" class="style19" colspan="2">
<asp:Label ID="lblError" runat="server" Text="" Visible="false" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2" align="center">
<asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
<cc1:ModalPopupExtender TargetControlID="btnShowPopup" ID="pnlModal_ModalPopupExtender"
runat="server" DynamicServicePath="" Enabled="True" BackgroundCssClass="modalBackground"
PopupControlID="pnlModal" CancelControlID="btnClose" DropShadow="true">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlModal" runat="server" Height="99px" Width="368px">
<table class="mainModalTable" cellpadding="0" height="98px" cellspacing="0" width="368px">
<tr>
<td class="mainModaTableTD">
<table class="mainModalInnerTable" cellspacing="0" cellpadding="0" height="98px"
width="368px">
<tr>
<td class="mainModalInnerTableTD">
<table border="0" cellspacing="0" cellpadding="4" style="width: 368px" height="98px">
<tr>
<td class="mainModalDraggablePanelTD" align="left">
<asp:Panel CssClass="mainModalDraggablePanel" ID="MPD" runat="server">
<span class="mainModalTitle" id="spanTitle">
<asp:Label ID="lblShowPopUpMsg" runat="server" Font-Size="10px" Text="" ForeColor="White"></asp:Label>
</span>
</asp:Panel>
</td>
<td align="right" class="mainModalDraggablePanelCloseTD">
<asp:ImageButton SkinID="CloseModal" runat="server" ID="clB" ImageUrl="~/Image/close.gif" />
</td>
</tr>
<tr>
<td class="mainModalContentsTD" colspan="2" align="center">
<div id="Div3">
</div>
</td>
</tr>
<tr>
<td class="mainModalContentsTD" colspan="2" align="center">
<div id="mainModalContents">
<asp:Label runat="server" ID="lblModalPopUp" Font-Bold="true" ForeColor="#3A6EA5"
Text="Are you sure to save all assets at once."></asp:Label>
</div>
</td>
</tr>
<tr>
<td class="mainModalContentsTD" colspan="2">
<div id="Div1">
</div>
</td>
</tr>
<tr>
<td class="mainModalContentsTD" colspan="2" align="center">
<div id="Div2">
<asp:Button ID="btnDone" runat="server" Text="Done" OnClick="btnDone_Click" CausesValidation="false"
CssClass="asset-viewer-btn" />
<asp:Button ID="btnClose" runat="server" Text="Close" CausesValidation="false" CssClass="asset-viewer-btn" />
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</asp:Panel>
<%----%>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<asp:UpdatePanel runat="server" ID="UPAssets">
<ContentTemplate>
<asp:DataList ID="dlstCreateAssets" runat="server" Width="100%" OnItemDataBound="dlstCreateAssets_ItemDataBound">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<div id="content_div">
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr>
<td align="left" style="padding-left: 30px;" id="id_td_pageHeading">
<asp:Label ID="lblName" runat="server" Text="Create/Modify Asset" Font-Bold="True"
Font-Size="Medium" CssClass="cslTitle"></asp:Label>
</td>
<td align="right">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
(*) Fields Mandatory
</td>
<td align="left">
<asp:Label ID="lblMsg" ForeColor="Red" runat="server" Text="" Visible="false"></asp:Label>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Item Id
</td>
<td align="left">
<asp:TextBox ID="txtItemId" runat="server" Width="170px" Text='<%#Eval("ItemId")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtItemId"
ErrorMessage="Enter ItemId" ValidationGroup="usctrl1btn2">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ValidationGroup="usctrl1btn2" ID="RegularExpressionValidator4"
runat="server" ErrorMessage="Enter only numeric value in Itemid." ControlToValidate="txtItemId"
SetFocusOnError="True" ValidationExpression="[0-9]*">*</asp:RegularExpressionValidator>
<asp:Button ID="btnCheckAvailability" runat="server" CssClass="asset-viewer-btn"
OnClick="btnCheckAvailability_Click" Text="Check Availability" />
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Type*
</td>
<td align="left">
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="AssetTypeName"
DataValueField="AssetTypeId" Width="170px">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="Drop1" Value='<%#Eval("AssetType")%>' />
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator2"
runat="server" ControlToValidate="DropDownList1" ErrorMessage="Select Type" InitialValue="-1">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
SubType*
</td>
<td align="left">
<asp:DropDownList ID="DropDownList2" runat="server" DataTextField="AssetSubTypeName"
DataValueField="AssetSubTypeId" Width="170px">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="Drop2" Value='<%#Eval("AssetSubType")%>' />
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator3"
runat="server" ControlToValidate="DropDownList2" ErrorMessage="Select SubType"
InitialValue="-1">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
AssetName*
</td>
<td align="left">
<asp:TextBox ID="txtAssetName" runat="server" Width="170px" Text='<%#Eval("AssetName")%>'></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Enter Asset Name" ControlToValidate="txtAssetName">*</asp:RequiredFieldValidator>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
Asset Data
</td>
<td align="left">
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("AssetData")%>' Height="114px"
TextMode="MultiLine" Width="249px"></asp:TextBox>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="left">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
<asp:ValidationSummary ValidationGroup="usctrl1" ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" />
</td>
<td align="left">
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" CssClass="asset-viewer-btn" />
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ShowMessageBox="True"
ShowSummary="False" ValidationGroup="usctrl1btn2" />
</td>
<td align="left">
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="left">
<asp:Label ID="lblshow" ForeColor="Red" runat="server" Visible="False"></asp:Label>
</td>
<td align="left">
</td>
</tr>
<tr>
<td align="left" style="padding-left: 30px;" class="style20">
</td>
<td align="right">
</td>
<td align="left">
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<%----%>
</asp:Content>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
//OdbcCommand command = new OdbcCommand();
//OdbcConnection myConnection = new OdbcConnection("DSN=Connection");
MySqlConnection myConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings["InventoryDBConnectionString"].ConnectionString);
MySqlCommand command;
MySqlDataReader reader;
DataSet ds;
//OdbcDataAdapter ad;
MySqlDataAdapter ad;
string counter = string.Empty;
string counter2 = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["tot"] = "0";
Int32 s = 0;
DataList(0);
}
}
protected void btnAddControl_Click(object sender, EventArgs e)
{
DataTable tbldata1 = new DataTable();
tbldata1.Columns.Add(new DataColumn("ItemId"));
tbldata1.Columns.Add(new DataColumn("AssetType"));
tbldata1.Columns.Add(new DataColumn("AssetSubType"));
tbldata1.Columns.Add(new DataColumn("AssetName"));
tbldata1.Columns.Add(new DataColumn("AssetData"));
tbldata1.Columns.Add(new DataColumn("Drop1"));
tbldata1.Columns.Add(new DataColumn("Drop2"));
int i = 0;
for (i = 0; i < dlstCreateAssets.Items.Count; i++)
{
DataRow dr = tbldata1.NewRow();
TextBox txtItemId = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtItemId"));
DropDownList DropDownList1 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList1"));
DropDownList DropDownList2 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList2"));
TextBox txtAssetName = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtAssetName"));
TextBox TextBox2 = (TextBox)(dlstCreateAssets.Items[i].FindControl("TextBox2"));
HiddenField Drop1 = (HiddenField)(dlstCreateAssets.Items[i].FindControl("Drop1"));
HiddenField Drop2 = (HiddenField)(dlstCreateAssets.Items[i].FindControl("Drop2"));
dr["ItemId"] = txtItemId.Text;
dr["AssetType"] = DropDownList1.SelectedItem.Value;
dr["AssetSubType"] = DropDownList2.SelectedItem.Value;
dr["AssetName"] = txtAssetName.Text;
dr["AssetData"] = TextBox2.Text;
dr["Drop1"] = DropDownList1.SelectedValue;
dr["Drop2"] = DropDownList2.SelectedValue;
tbldata1.Rows.Add(dr);
}
int u = 1;
Int32 o = txtAdd.Text != "" ? Convert.ToInt32(txtAdd.Text) : 1;
i = 0;
DataTable tbldata = (DataTable)ViewState["items"];
while (i < o)
{
DataRow dr = tbldata1.NewRow();
tbldata1.Rows.Add(dr);
i++;
u++;
}
ViewState["items"] = tbldata1;
DataList(1);
}
protected void DataList(int Num)
{
DataTable tbldata = null;
if (ViewState["items"] == null)
{
tbldata = new DataTable();
tbldata.Columns.Add(new DataColumn("ItemId"));
tbldata.Columns.Add(new DataColumn("AssetType"));
tbldata.Columns.Add(new DataColumn("AssetSubType"));
tbldata.Columns.Add(new DataColumn("AssetName"));
tbldata.Columns.Add(new DataColumn("AssetData"));
tbldata.Columns.Add(new DataColumn("Drop1"));
tbldata.Columns.Add(new DataColumn("Drop2"));
}
else
{
tbldata = (DataTable)ViewState["items"];
}
if (Num == 0)
{
DataRow dr = tbldata.NewRow();
tbldata.Rows.Add(dr);
Num = 1;
}
ViewState["items"] = tbldata;
dlstCreateAssets.DataSource = tbldata;
dlstCreateAssets.DataBind();
}
protected void dlstCreateAssets_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txtItemId = (TextBox)e.Item.FindControl("txtItemId");
DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
DropDownList DropDownList2 = (DropDownList)e.Item.FindControl("DropDownList2");
TextBox txtAssetName = (TextBox)e.Item.FindControl("txtAssetName");
TextBox TextBox2 = (TextBox)e.Item.FindControl("TextBox2");
HiddenField Drop1 = (HiddenField)e.Item.FindControl("Drop1");
HiddenField Drop2 = (HiddenField)e.Item.FindControl("Drop2");
string str = "select AssetTypeId,AssetTypeName as AssetType from assettypes";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//command = new OdbcCommand(str, myConnection);
command = new MySqlCommand(str, myConnection);
ds = new DataSet();
ad = new MySqlDataAdapter(str, myConnection);
ad.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataValueField = "AssetTypeId";
DropDownList1.DataTextField = "AssetType";
DropDownList1.DataBind();
if (Drop1.Value != "" && Drop1.Value != "-1")
{
DropDownList1.SelectedValue = Drop1.Value;
}
DropDownList1.Items.Insert(0, new ListItem("-- Select Type --", "-1"));
myConnection.Close();
string str1 = "select AssetSubTypeId,AssetSubTypeName as AssetSubType from AssetSubtypes";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
command = new MySqlCommand(str1, myConnection);
ds = new DataSet();
ad = new MySqlDataAdapter(str1, myConnection);
ad.Fill(ds);
DropDownList2.DataSource = ds;
DropDownList2.DataValueField = "AssetSubTypeId";
DropDownList2.DataTextField = "AssetSubType";
DropDownList2.DataBind();
if (Drop2.Value != "" && Drop2.Value!="-1")
{
DropDownList2.SelectedValue = Drop2.Value;
}
DropDownList2.Items.Insert(0, new ListItem("-- Select SubType --", "-1"));
myConnection.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable tbldata3 = new DataTable();
tbldata3.Columns.Add(new DataColumn("ItemId"));
tbldata3.Columns.Add(new DataColumn("AssetType"));
tbldata3.Columns.Add(new DataColumn("AssetSubType"));
tbldata3.Columns.Add(new DataColumn("AssetName"));
tbldata3.Columns.Add(new DataColumn("AssetData"));
tbldata3.Columns.Add(new DataColumn("Drop1"));
tbldata3.Columns.Add(new DataColumn("Drop2"));
//Selecting Index of Datalist
Button btnSave = (Button)sender;
DataListItem item = (DataListItem)btnSave.NamingContainer;
int index = item.ItemIndex;
DataRow dr = tbldata3.NewRow();
TextBox txtItemId = (TextBox)(dlstCreateAssets.Items[index].FindControl("txtItemId"));
DropDownList DropDownList1 = (DropDownList)(dlstCreateAssets.Items[index].FindControl("DropDownList1"));
DropDownList DropDownList2 = (DropDownList)(dlstCreateAssets.Items[index].FindControl("DropDownList2"));
TextBox txtAssetName = (TextBox)(dlstCreateAssets.Items[index].FindControl("txtAssetName"));
TextBox TextBox2 = (TextBox)(dlstCreateAssets.Items[index].FindControl("TextBox2"));
Label lblMsg = (Label)(dlstCreateAssets.Items[index].FindControl("lblMsg"));
HiddenField Drop1 = (HiddenField)(dlstCreateAssets.Items[index].FindControl("Drop1"));
HiddenField Drop2 = (HiddenField)(dlstCreateAssets.Items[index].FindControl("Drop2"));
int index2 = index + 1;
if (txtAssetName.Text == "")
{
lblMsg.Visible = true;
lblMsg.Text = "Enter AssetName in '" + index2 + "' asset table";
return;
}
else if (DropDownList1.SelectedItem.Text == "-- Select Type --")
{
lblMsg.Visible = true;
lblMsg.Text = "Select Assettype in '" + index2 + "' asset table";
return;
}
else if (DropDownList2.SelectedItem.Text == "-- Select SubType --")
{
lblMsg.Visible = true;
lblMsg.Text = "Enter AssetSubType in '" + index2 + "' asset table";
return;
}
else
{
dr["ItemId"] = txtItemId.Text;
dr["AssetType"] = DropDownList1.SelectedItem.Value;
dr["AssetSubType"] = DropDownList2.SelectedItem.Value;
dr["AssetName"] = txtAssetName.Text;
dr["AssetData"] = TextBox2.Text;
dr["Drop1"] = DropDownList1.SelectedValue;
dr["Drop2"] = DropDownList2.SelectedValue;
tbldata3.Rows.Add(dr);
string sqlry = "Insert into assets (AssetType,AssetSubType,AssetName,AssetFilePath,AssetData) values ('" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','" + txtAssetName.Text + "','Test','" + TextBox2.Text + "')";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand myCommand = new OdbcCommand(sqlry, myConnection);
command = new MySqlCommand(sqlry, myConnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlry;
command.ExecuteNonQuery();
myConnection.Close();
lblMsg.Visible = true;
lblMsg.Text = "Asset Created Successfully.";
CheckItemid(txtItemId.Text);
if (txtItemId.Text != "" && counter2 != string.Empty)
{
GetMaxAssetId();
string strItemId = "Insert into itemstoassetmapping (ItemId,AssetId) values('" + txtItemId.Text + "','" + Session["MaxAssetId"] + "')";
//OdbcCommand mycommand = new OdbcCommand(strItemId, myConnection);
command = new MySqlCommand(strItemId, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
command.CommandType = CommandType.Text;
command.CommandText = strItemId;
command.ExecuteNonQuery();
myConnection.Close();
lblMsg.Visible = true;
lblMsg.Text = "Asset with ItemId Created Successfully.";
txtAssetName.Text = "";
counter = string.Empty;
}
else
{
if (txtItemId.Text == "")
{
lblMsg.Visible = true;
lblMsg.Text = "Asset Created Successfully.";
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "Asset Created Successfully but this itemid does not exists in '" + index2 + "' Asset table.";
txtAssetName.Text = "";
}
}
}
}
private String CheckItemid(string itemid)
{
try
{
string check = "Select * from itemslist where ItemId='" + itemid + "'";
//OdbcCommand mycommand = new OdbcCommand(check, myConnection);
command = new MySqlCommand(check, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
reader = command.ExecuteReader();
if (reader.Read())
{
counter2 = reader["ItemId"].ToString();
}
reader.Close();
myConnection.Close();
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.Message;
}
return counter2;
}
protected void btnCheckAvailability_Click(object sender, EventArgs e)
{
string counterCheck = string.Empty;
Button btnCheckAvailability = (Button)sender;
DataListItem item = (DataListItem)btnCheckAvailability.NamingContainer;
int index = item.ItemIndex;
DataTable tbldataCheck = new DataTable();
tbldataCheck.Columns.Add(new DataColumn("ItemId"));
DataRow dr = tbldataCheck.NewRow();
TextBox txtItemId = (TextBox)(dlstCreateAssets.Items[index].FindControl("txtItemId"));
Label lblMsg = (Label)(dlstCreateAssets.Items[index].FindControl("lblMsg"));
if (txtItemId.Text == "")
{
lblMsg.Visible = true;
lblMsg.Text = "Enter ItemId";
return;
}
else
{
//if (txtItemId.Text != "")
//{
// Regex txtItemnumbers = new Regex(@"[0-9]*");
// bool status = txtItemnumbers.IsMatch(txtItemId.Text);
//}
try
{
string check = "Select * from itemslist where ItemId='" + txtItemId.Text + "'";
//OdbcCommand mycommand = new OdbcCommand(check, myConnection);
command = new MySqlCommand(check, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
reader = command.ExecuteReader();
if (reader.Read())
{
counterCheck = reader["ItemId"].ToString();
}
reader.Close();
myConnection.Close();
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
}
if (counterCheck == string.Empty)
{
lblMsg.Visible = true;
lblMsg.Text = "This itemId does not exits,select another ItemId";
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "This itemId exits.";
}
}
protected void btnSaveAllRecords_Click(object sender, EventArgs e)
{
if (!CheckValidation())
{
lblError.Visible = true;
lblError.Text = "One or more entries in Asset table are incorrect.";
return;
}
else
{
lblError.Visible = false;
lblShowPopUpMsg.Text = "The page at " + Request.Url.GetLeftPart(UriPartial.Authority) + " says...";
pnlModal_ModalPopupExtender.Show();
//InsertAllTableData();
}
}
private void InsertAllTableData()
{
DataTable tblInsertdata = new DataTable();
tblInsertdata.Columns.Add(new DataColumn("ItemId"));
tblInsertdata.Columns.Add(new DataColumn("AssetType"));
tblInsertdata.Columns.Add(new DataColumn("AssetSubType"));
tblInsertdata.Columns.Add(new DataColumn("AssetName"));
tblInsertdata.Columns.Add(new DataColumn("AssetData"));
int countrows = 1;
for (int i = 0; i < dlstCreateAssets.Items.Count; i++)
{
TextBox txtItemId = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtItemId"));
DropDownList DropDownList1 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList1"));
DropDownList DropDownList2 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList2"));
TextBox txtAssetName = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtAssetName"));
TextBox TextBox2 = (TextBox)(dlstCreateAssets.Items[i].FindControl("TextBox2"));
DataRow dr = tblInsertdata.NewRow();
dr["ItemId"] = txtItemId.Text;
dr["AssetType"] = DropDownList1.SelectedItem.Value;
dr["AssetSubType"] = DropDownList2.SelectedItem.Value;
dr["AssetName"] = txtAssetName.Text;
dr["AssetData"] = TextBox2.Text;
tblInsertdata.Rows.Add(dr);
string sqlry = "Insert into assets (AssetType,AssetSubType,AssetName,AssetFilePath,AssetData) values ('" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','" + txtAssetName.Text + "','Test','" + TextBox2.Text + "')";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand myCommand = new OdbcCommand(sqlry, myConnection);
command = new MySqlCommand(sqlry, myConnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlry;
command.ExecuteNonQuery();
myConnection.Close();
CheckItemidExistence(txtItemId.Text);
if (txtItemId.Text != "" && counter != string.Empty)
{
GetMaxAssetId();
string strItemId = "Insert into itemstoassetmapping (ItemId,AssetId) values('" + txtItemId.Text + "','" + Session["MaxAssetId"] + "')";
//OdbcCommand mycommand = new OdbcCommand(strItemId, myConnection);
command = new MySqlCommand(strItemId, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
command.CommandType = CommandType.Text;
command.CommandText = strItemId;
command.ExecuteNonQuery();
myConnection.Close();
lblError.Visible = true;
lblError.Text = "Asset with ItemId Created Successfully.";
txtAssetName.Text = "";
counter = string.Empty;
countrows++;
}
else
{
if (txtItemId.Text == "")
{
lblError.Visible = true;
lblError.Text = "Asset Created Successfully.";
}
else
{
lblError.Visible = true;
lblError.Text = "Asset Created Successfully but this itemid does not exists in '" + countrows + "' Asset table.";
txtAssetName.Text = "";
}
}
}
}
private bool CheckValidation()
{
DataTable tbldataNew = new DataTable();
tbldataNew.Columns.Add(new DataColumn("ItemId"));
tbldataNew.Columns.Add(new DataColumn("AssetType"));
tbldataNew.Columns.Add(new DataColumn("AssetSubType"));
tbldataNew.Columns.Add(new DataColumn("AssetName"));
tbldataNew.Columns.Add(new DataColumn("AssetData"));
bool validate = false;
for (int i = 0; i < dlstCreateAssets.Items.Count; i++)
{
validate = true;
TextBox txtItemId = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtItemId"));
DropDownList DropDownList1 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList1"));
DropDownList DropDownList2 = (DropDownList)(dlstCreateAssets.Items[i].FindControl("DropDownList2"));
TextBox txtAssetName = (TextBox)(dlstCreateAssets.Items[i].FindControl("txtAssetName"));
TextBox TextBox2 = (TextBox)(dlstCreateAssets.Items[i].FindControl("TextBox2"));
DataRow dr = tbldataNew.NewRow();
dr["ItemId"] = txtItemId.Text;
dr["AssetType"] = DropDownList1.SelectedItem.Value;
dr["AssetSubType"] = DropDownList2.SelectedItem.Value;
dr["AssetName"] = txtAssetName.Text;
dr["AssetData"] = TextBox2.Text;
tbldataNew.Rows.Add(dr);
if (txtItemId.Text.Trim() == "")
{
validate = false;
break;
}
else if (txtAssetName.Text == "")
{
validate = false;
break;
}
//else if (TextBox2.Text == "")
//{
// validate = false;
// break;
//}
else if (DropDownList1.SelectedItem.Text == "-- Select Type --")
{
validate = false;
break;
}
else if (DropDownList2.SelectedItem.Text == "-- Select SubType --")
{
validate = false;
break;
}
//return validate;
}
if (validate == false)
return validate;
ViewState["allTblData"] = tbldataNew;
return validate;
}
protected String CheckItemidExistence(string number)
{
try
{
string check = "Select * from itemslist where ItemId='" + number + "'";
//OdbcCommand mycommand = new OdbcCommand(check, myConnection);
command = new MySqlCommand(check, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
reader = command.ExecuteReader();
if (reader.Read())
{
counter = reader["ItemId"].ToString();
}
reader.Close();
myConnection.Close();
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.Message;
}
return counter;
}
protected void GetMaxAssetId()
{
try
{
string StrMax = "select Max(AssetId)as AssetId from assets";
//OdbcCommand mycommand = new OdbcCommand(StrMax, myConnection);
command = new MySqlCommand(StrMax, myConnection);
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
reader = command.ExecuteReader();
if (reader.Read())
{
Session["MaxAssetId"] = reader["AssetId"].ToString();
}
reader.Close();
myConnection.Close();
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.Message;
}
}
protected void btnDone_Click(object sender, EventArgs e)
{
InsertAllTableData();
}
//protected void btnSubmit_Click(object sender, EventArgs e)
//{
// System.Threading.Thread.Sleep(500);
// ProgressBarModalPopupExtender.Hide();
//}
}