Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
I'm doing a small trail to get Any Selected MDb tables in to my datagridview in c#.
 
What I need is when I click on Add button it should return a row with empty cells in the datagridview and it should reflect in database.
 
P.Rupesh
Posted 15-Jan-10 19:02pm
Edited 16-Sep-10 22:32pm
v4
Comments
Dalek Dave at 17-Sep-10 3:33am
   
Edited for Grammar, Readability and Removal of Email Address.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hey...
To add new row do like this....
 
DataSet ds = (DataSet)GV.DataSource;
 
DataRow dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
 
GV.DataSource = ds;
GV.DataBind();
 
check it out..............:thumbsup:
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

For add a new row use the below code,
 
    DataTable dttable = new DataTable();
    DataColumn column;
    DataRow row; 
 
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.ColumnName = "Empid";
    dttable.Columns.Add(column);
 
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName = "EmpName";
    dttable.Columns.Add(column);
 
    row = table.NewRow();
    row["id"] = 1;
    row["item"] = "Albert";
    dttable.Rows.Add(row);
 
    Gridview1.DataSource = dttable;
now you try next step for update to db.
  Permalink  
Comments
Dalek Dave at 17-Sep-10 3:33am
   
Good Call.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

DataRow dr=dt.Tables[0].NewRow();
dt.Tables[0].Rows.Add(dr);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

DataSet ds = (DataSet)gridView1.DataSource;
 
DataRow dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
 
gridView1.DataSource = ds;
gridView1.DataBind();
  Permalink  
v2
Comments
Smithers-Jones at 7-Dec-11 6:43am
   
Added code-block. By the way, did you see that the original question if nearly two years old?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

simply do like the following
 
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
 
string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
 
More examples....C# Datagridview tutorial
 
Vyar
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Updating the gridview probably won't work if it's data bound. Instead, you should just update your data source, then refresh the data from Access, thus updating your UI.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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>
                                &nbsp;
                            </td>
                            <td align="left">
                                &nbsp;
                            </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">
                                &nbsp;
                            </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">
                                &nbsp;
                            </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" />&nbsp;&nbsp;
                                                                            <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>
                                <%--<asp:Panel ID="pnlModal" runat="server" CssClass="modalPopup" Height="69px" Width="368px">
                                    <br />
                                    <asp:Label runat="server" ID="lblModalPopUp" Text="Are u sure to save all assets at once."
                                        ForeColor="Red"></asp:Label>
                                    <br />
                                    <br />
                                    <asp:Button ID="btnDone" runat="server" Text="Done" OnClick="btnDone_Click" CausesValidation="false"
                                        CssClass="asset-viewer-btn" />&nbsp;&nbsp;
                                    <asp:Button ID="btnClose" runat="server" Text="Close" CausesValidation="false" CssClass="asset-viewer-btn" />
                                    &nbsp;&nbsp;
                                </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">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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">
                                                &nbsp;
                                            </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>
                                                &nbsp;<asp:Button ID="btnCheckAvailability" runat="server" CssClass="asset-viewer-btn"
                                                    OnClick="btnCheckAvailability_Click" Text="Check Availability" />
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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")%>' />
                                                &nbsp;<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator2"
                                                    runat="server" ControlToValidate="DropDownList1" ErrorMessage="Select Type" InitialValue="-1">*</asp:RequiredFieldValidator>
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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")%>' />
                                                &nbsp;<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator3"
                                                    runat="server" ControlToValidate="DropDownList2" ErrorMessage="Select SubType"
                                                    InitialValue="-1">*</asp:RequiredFieldValidator>
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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>
                                                &nbsp;<asp:RequiredFieldValidator ValidationGroup="usctrl1" ID="RequiredFieldValidator1"
                                                    runat="server" ErrorMessage="Enter Asset Name" ControlToValidate="txtAssetName">*</asp:RequiredFieldValidator>
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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">
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="padding-left: 30px;" class="style20">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </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">
                                                &nbsp;
                                            </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">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="padding-left: 30px;" class="style20">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                <asp:Label ID="lblshow" ForeColor="Red" runat="server" Visible="False"></asp:Label>
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="padding-left: 30px;" class="style20">
                                                &nbsp;
                                            </td>
                                            <td align="right">
                                                &nbsp;
                                            </td>
                                            <td align="left">
                                                &nbsp;
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
        </ContentTemplate>
    </asp:UpdatePanel>
    <%--<asp:UpdatePanel ID="UpdatePanel12" runat="server">
        <ContentTemplate>
            <div>
                <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" OnClientClick="StartProgressBar()"
                    runat="server" Text="Submit Time" Width="170px" />
                <cc1:ModalPopupExtender ID="ProgressBarModalPopupExtender" runat="server" BackgroundCssClass="ModalBackground"
                    BehaviorID="ProgressBarModalPopupExtender" TargetControlID="hiddenField" PopupControlID="Panel1" />
                <asp:Panel ID="Panel1" runat="server" Style="display: none; background-color: #C0C0C0;">
                    <img src="Image/indicator.gif" alt="" />
                </asp:Panel>
                <asp:HiddenField ID="hiddenField" runat="server" />
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <script language="javascript" type="text/javascript">
        function StartProgressBar() {
            var myExtender = $find('ProgressBarModalPopupExtender');
            myExtender.show();
            return true;
        }</script>--%>
</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();
//}
}
  Permalink  
Comments
Dalek Dave at 17-Sep-10 3:33am
   
Wow.
Vigneshb6 at 25-Oct-10 7:13am
   
What is this?
Nirav Prabtani at 18-Jul-13 6:08am
   
great work....!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 22 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100