In order to retrieve data, user have to select a value from a dropdownlist (DropDownListTDRCode). Below I show you some snippets of my codes:
.aspx codes
<table>
<tr class="box">
<td class="gap"></td>
<td class="lbl"><asp:Label ID="LabelTraderCode" runat="server" Text="Trader Code" CssClass="label"></asp:Label></td>
<td class="inp"><asp:TextBox ID="TextBoxTDRCode" runat="server" CssClass="input"></asp:TextBox></td>
<td class="inp"><asp:DropDownList ID="DropDownListTDRCode" runat="server" AutoPostBack="true" AppendDataBoundItems="true" CssClass="dropdown" OnSelectedIndexChanged="DropDownListTDRCode_SelectedIndexChanged"></asp:DropDownList></td>
<td class="lbl"><asp:Label ID="LabelTraderName" runat="server" Text="Trader Name" CssClass="label"></asp:Label></td>
<td class="inp"><asp:TextBox ID="TextBoxTDRName" runat="server" CssClass="input"></asp:TextBox></td>
<td class="gap">
</tr>
</table>
.aspx.cs:
//GLType
protected void GridViewSelection_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownListGLType = (e.Row.FindControl("DropDownListGLType") as DropDownList);
//DropDownListGLType.Items.Clear();
DropDownListGLType.DataSource = dt;
DropDownListGLType.DataTextField = "DESCR";
DropDownListGLType.DataValueField = "PARA_CODE";
DropDownListGLType.DataBind();
DropDownListGLType.Items.Insert(0, DefaultItem);
//ViewState["CurrentTable"] = dt;
}
}
protected void DropDownListGLType_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvrow = (sender as DropDownList).NamingContainer as GridViewRow;
rowindex = (gvrow as GridViewRow).RowIndex;
ViewState["rowindex"] = rowindex;
int passing = Convert.ToInt32(ViewState["rowindex"]);
GridViewRow row2 = GridViewSelection.Rows[passing];
string QueryCUR = "SELECT GL_ACCNO, ACC_GROUP, DESCR, seq_no FROM BOS_M_GL where (acc_group = 'BC' and seq_no in ('90', '100')) or (acc_group = 'P10' and seq_no = '1')";
string QueryDEP = "SELECT GL_ACCNO, ACC_GROUP, descr, seq_no FROM BOS_M_GL where acc_group = 'BC' and seq_no = '120'";
string QueryRET = "SELECT GL_ACCNO, ACC_GROUP, descr, seq_no FROM BOS_M_GL where acc_group = 'BC' and seq_no = '135'";
string QueryEXP = "SELECT GL_ACCNO, ACC_GROUP, descr, seq_no FROM BOS_M_GL where acc_group = 'P63' and seq_no = '1'";
OracleDataAdapter adapterCUR = new OracleDataAdapter(QueryCUR, con);
OracleDataAdapter adapterDEP = new OracleDataAdapter(QueryDEP, con);
OracleDataAdapter adapterRET = new OracleDataAdapter(QueryRET, con);
OracleDataAdapter adapterEXP = new OracleDataAdapter(QueryEXP, con);
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("GL_ACCNO"), new DataColumn("DESCR"), new DataColumn("ACC_GROUP"), new DataColumn("SEQ_NO") });
GridViewRow row = GridViewSelection.SelectedRow;
DropDownList DropDownListGLType = sender as DropDownList;
if (DropDownListGLType.SelectedValue == "CUR")
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
adapterCUR.Fill(ds, "BOS_M_GL");
}
else if (DropDownListGLType.SelectedValue == "DEP")
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
adapterDEP.Fill(ds, "BOS_M_GL");
}
else if (DropDownListGLType.SelectedValue == "RET")
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
adapterRET.Fill(ds, "BOS_M_GL");
}
else if (DropDownListGLType.SelectedValue == "EXP")
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
adapterEXP.Fill(ds, "BOS_M_GL");
}
else
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
GridView2.Visible = false;
return;
}
if (ds.Tables.Contains("BOS_M_GL"))
{
DataView BMGContent = ds.Tables["BOS_M_GL"].DefaultView;
if (BMGContent.Count > 0)
{
GridView2.Visible = true;
GridView2.DataSource = BMGContent;
GridView2.DataBind();
}
else
{
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = "";
GridView2.Visible = false;
}
}
GridView2.DataSource = ds;
GridView2.DataBind();
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
PassingGLAccNo();
}
protected void PassingGLAccNo()
{
int passing = Convert.ToInt32(ViewState["rowindex"]);
GridViewRow row2 = GridViewSelection.Rows[passing];
(row2.FindControl("TextBoxGLAccNo") as TextBox).Text = GridView2.SelectedRow.Cells[0].Text;
}
//GLType
Below is my save button which can be used when inserting and updating data.
//Retrieve Data
protected void DropDownTDRCode()
{
string QueryTDRCode = "SELECT TDR_CODE FROM BOS_M_TRADER ORDER BY TDR_CODE";
OracleDataAdapter AdapterTDRCode = new OracleDataAdapter(QueryTDRCode, con);
DataTable dtTDRCode = new DataTable();
AdapterTDRCode.Fill(dtTDRCode);
DropDownListTDRCode.Items.Insert(0, DefaultItem);
DropDownListTDRCode.DataSource = dtTDRCode;
DropDownListTDRCode.DataTextField = "TDR_CODE";
DropDownListTDRCode.DataValueField = "TDR_CODE";
DropDownListTDRCode.DataBind();
}
protected void DropDownListTDRCode_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
RetrieveData();
con.Close();
}
protected void RetrieveData()
{
RetrieveTradingInfo();
RetrieveGLInfo();
}
protected void RetrieveLocalData()
{
TextBoxTDRCode.Text = L_TDRCode;
TextBoxTDRName.Text = L_TDRName;
//GLInfo
//DropDownListGLType.SelectedValue = L_GLType;
//GLInfo
}
protected void RetrieveTradingInfo()
{
string QueryDropDownList = "SELECT * FROM BOS_M_TRADER WHERE TDR_CODE = :TDR_CODE ORDER BY TDR_CODE";
using (OracleCommand cmd = new OracleCommand(QueryDropDownList, con))
{
cmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = DropDownListTDRCode.SelectedValue;
using (OracleDataAdapter adapter = new OracleDataAdapter(cmd))
{
adapter.Fill(dt);
try
{
if (dt.Rows.Count > 0)
{
L_TDRCode = dt.Rows[0]["TDR_CODE"].ToString();
L_TDRName = dt.Rows[0]["TDR_NAME"].ToString();
//GLInfo
//L_GLType = dt.Rows[0]["GL_TYPE"].ToString();
//GLInfo
}
else
{
L_TDRCode = "";
L_TDRName = "";
}
RetrieveLocalData();
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
}
}
protected void RetrieveGLInfo()
{
string QueryGLType = "SELECT * FROM BOS_M_TRADER_GL WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand cmd = new OracleCommand(QueryGLType, con))
{
cmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = DropDownListTDRCode.SelectedValue;
using (OracleDataAdapter adapter = new OracleDataAdapter(cmd))
{
adapter.Fill(dt);
try
{
if (dt.Rows.Count > 0)
{
}
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
}
}
//Retrieve Data
I created 2 GriedViews. The first GriedView (GridViewSelection) stores DropDownListGLType where when user select a value, the second GridView (GridView2) will show up below GridViewSelection. Inside GridView2, user can select whatever row (each row has a GL_ACCNO). After selecting a row, the value of GL_ACCNO will appear inside a TextBoxGLAccNo which is located inside the GridViewSelection.
.aspx.cs codes:
<div class="folder-content">
<br />
<div>
<asp:GridView ID="GridViewSelection" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridViewSelection_RowDataBound" Visible="true" ShowFooter="false" >
<Columns>
<asp:TemplateField HeaderText="GL Type">
<ItemTemplate>
<asp:DropDownList ID="DropDownListGLType" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownListGLType_SelectedIndexChanged" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GL Acc No">
<ItemTemplate>
<asp:TextBox ID="TextBoxGLAccNo" runat="server"></asp:TextBox>
</ItemTemplate>
<%--<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAddRow" runat="server" Text="Add another row" OnClick="ButtonAddRow_Click" />
</FooterTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<div>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="GL_ACCNO" HeaderText="GL Acc No" SortExpression="GL_ACCNO" />
<asp:BoundField DataField="DESCR" HeaderText="Description" SortExpression="DESCR" />
<asp:BoundField DataField="ACC_GROUP" HeaderText="Acc Group" SortExpression="ACC_GROUP" />
<asp:BoundField DataField="SEQ_NO" HeaderText="Seq No" SortExpression="SEQ_NO" />
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Select" />
</Columns>
</asp:GridView>
</div>
</div>
protected void ButtonSave_Click(object sender, EventArgs e)
{
con.Open();
string QueryCheckExistingTDRCode = "SELECT COUNT(*) FROM BOS_M_TRADER WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand CheckExistingTDRCodeCmd = new OracleCommand(QueryCheckExistingTDRCode, con))
{
CheckExistingTDRCodeCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = DropDownListTDRCode.SelectedValue;
int count = Convert.ToInt32(CheckExistingTDRCodeCmd.ExecuteScalar());
if (count == 0)
{
InsertData();
}
else
{
UpdateData();
}
}
con.Close();
}
protected void InsertData()
{
string QueryInsertNewData = "INSERT INTO BOS_M_TRADER (TDR_CODE, TDR_NAME) " +
//GAP
"VALUES (:TDR_CODE, :TDR_NAME)";
using (OracleCommand InsertNewDataCmd = new OracleCommand(QueryInsertNewData, con))
{
using (OracleDataAdapter AdapterInsert = new OracleDataAdapter(InsertNewDataCmd))
{
AdapterInsert.Update(dt);
try
{
InsertNewDataCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = TextBoxTDRCode.Text;
InsertNewDataCmd.Parameters.Add("TDR_NAME", OracleDbType.Varchar2).Value = TextBoxTDRName.Text;
InsertNewDataCmd.ExecuteNonQuery();
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
}
}
protected void UpdateData()
{
string QueryUpdateData = "UPDATE BOS_M_TRADER SET TDR_CODE = :TDR_CODE, TDR_NAME = :TDR_NAME WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand UpdateCmd = new OracleCommand(QueryUpdateData, con))
{
using (OracleDataAdapter AdapterUpdate = new OracleDataAdapter(UpdateCmd))
{
AdapterUpdate.Update(dt);
try
{
UpdateCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = TextBoxTDRCode.Text;
UpdateCmd.Parameters.Add("TDR_NAME", OracleDbType.Varchar2).Value = TextBoxTDRName.Text;
UpdateCmd.ExecuteNonQuery();
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
}
}
//Save Data
How do I retrieve the data of DropDownListGLType and TextBoxGLAccNo from the my database and how do insert and update the data into my database also?