passing string value fill dropdown from database - c#

I need to fill the dropdown value from database passing string value.here i am gettin filled dataset from database but it is not binding so that i am unable to fill properly the dropdown and select items from dropdown.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strCreatedId = string.Empty;
strCreatedId = "2";
fillgroupname(id_ddlgroupname,strCreatedId);
}
}
public void fillgroupname(DropDownList id_ddlgroupname, string strCreatedId)
{
DataSet dsgroup = new DataSet();
dsgroup = objUser.GetFillGroup(strCreatedId);
if (dsgroup.Tables.Count > 0)
{
if (dsgroup.Tables[0].Rows.Count > 0)
{
this.id_ddlgroupname.DataSource = dsgroup;
this.id_ddlgroupname.DataTextField = "c_group_name";
this.id_ddlgroupname.DataValueField = "c_group_name";
this.id_ddlgroupname.DataBind();
this.id_ddlgroupname.Items.Insert(0, "--Select--");
}
}
I will upload image as it appears.
Image1:
Image2:
Image3:
like this the dropdown issue i am facing i am not able to know where i am going wrong.pls help me.

Use this code
public void fillgroupname(DropDownList id_ddlgroupname, string strCreatedId)
{
DataSet dsgroup = new DataSet();
dsgroup = objUser.GetFillGroup(strCreatedId);
id_ddlgroupname.DataSource = dsgroup;
id_ddlgroupname.DataTextField = "c_group_name";
id_ddlgroupname.DataValueField = "c_group_name";
id_ddlgroupname.DataBind();
ListItem li = new ListItem("--Select--","0");
id_ddlgroupname.Items.Insert(0, li);
}
This is working for me, I hope it will be use full for you.

sorry. I got solved my issue it was problem in my validation using jquery.

Related

Create buttons dynamically from code behind in ASP.NET

I'm quite new to ASP.NET and I need your help.
I'm programming on an application which should help to fix frequent issues. Users can click the displayed cases if it describes their problem. The application searches for more cases or displays a possible solution.
Now what I need for this is some code which creates the buttons dynamically. I googled some ideas and created some code, however I was not able to get it to work.
It works to create the first selection of buttons with the Default_Load method. Also the OnClick event (ButtonClick_System) works fine which means I get the next selection.
From here it starts messing around. The dynamic buttons created in ButtonClick_System don't have a working OnClick action.
Instead of proceeding with ButtonClick_Question (because of btn_system.Command += ButtonClick_Question; in ButtonClick_System) it seems like it just loads the homepage (maybe something wrong with Page_Load?).
The application should do ButtonClick_Question until no more datasets available in database.
I got the following code:
using System;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using Oracle.DataAccess.Client;
namespace Application
{
public partial class _default : System.Web.UI.Page
{
// Variables
private string app_name = "Application";
// ----- Page_Load ----- //
protected void Page_Load(object sender, EventArgs e)
{
Default_Load();
Session["Application"] = app_name;
}
// ----- Methods ----- //
// Load homepage
public void Default_Load()
{
pnl_default.Visible = true;
pnl_content.Visible = false;
HtmlGenericControl html_default = new HtmlGenericControl();
html_default.TagName = "div";
string cmdString = "(...)";
DataTable dtSystems = OraQueryData(cmdString);
foreach (DataRow dtRow in dtSystems.Rows)
{
int system_id = Convert.ToInt32(dtRow["SYSTEM_ID"]);
string system_name = Convert.ToString(dtRow["SYSTEM_NAME"]);
var btn_system = new Button
{
ID = "btn_" + system_name,
Text = system_name,
CssClass = "sys_buttons"
};
btn_system.Command += ButtonClick_System;
btn_system.CommandArgument = Convert.ToString(system_id);
html_default.Controls.Add(btn_system);
}
plh_default.Controls.Clear();
plh_default.Controls.Add(html_default);
}
// Button OnClick Events
protected void ButtonClick_System(object sender, CommandEventArgs e)
{
pnl_default.Visible = false;
pnl_content.Visible = true;
HtmlGenericControl html_questions = new HtmlGenericControl();
html_questions.TagName = "div";
int system_id = Convert.ToInt32(e.CommandArgument);
string cmdString = "(...)";
DataTable dtQuestions = OraQueryData(cmdString);
foreach (DataRow dtRow in dtQuestions.Rows)
{
string question_id = Convert.ToString(dtRow["FRAGE_ID"]);
string question_text = Convert.ToString(dtRow["FRAGE_TEXT"]);
var btn_system = new Button
{
ID = "btn_question" + question_id,
Text = question_text,
CssClass = "quest_buttons"
};
btn_system.Command += ButtonClick_Question;
btn_system.CommandArgument = Convert.ToString(system_id);
html_questions.Controls.Add(btn_system);
}
plh_content.Controls.Clear();
plh_content.Controls.Add(html_questions);
}
protected void ButtonClick_Question(object sender, CommandEventArgs e)
{
pnl_default.Visible = false;
pnl_content.Visible = true;
HtmlGenericControl html_ChildQuestions = new HtmlGenericControl();
html_ChildQuestions.TagName = "div";
int parent_id = Convert.ToInt32(e.CommandArgument);
string cmdString = "(...)";
DataTable dtChildQuestions = OraQueryData(cmdString);
foreach (DataRow dtRow in dtChildQuestions.Rows)
{
string question_id = Convert.ToString(dtRow["FRAGE_ID"]);
string question_text = Convert.ToString(dtRow["FRAGE_TEXT"]);
var btn_system = new Button
{
ID = "btn_question" + question_id,
Text = question_text,
CssClass = "quest_buttons"
};
btn_system.Command += ButtonClick_Question;
btn_system.CommandArgument = question_id;
html_ChildQuestions.Controls.Add(btn_system);
}
plh_content.Controls.Clear();
plh_content.Controls.Add(html_ChildQuestions);
}
// ----- Oracle Data Query Methods ----- //
// Create and execute query on database
public static DataTable OraQueryData(string cmdString)
{
string conString = ConfigurationManager.AppSettings["Connection"];
OracleConnection oraCon = new OracleConnection(conString);
OracleCommand oraCmd = new OracleCommand(cmdString, oraCon);
OracleDataAdapter oraDtAd = new OracleDataAdapter(oraCmd.CommandText, oraCon);
DataTable dt = new DataTable();
oraCon.Open();
oraDtAd.Fill(dt);
oraCon.Close();
return dt;
}
}
}
If I've understood the issue correctly I think you're using the wrong controls for the wrong usages.
What I'd suggest you need to do is bind the collection of FAQ records to a repeater or some other data set display control. You can then have an event on the repeater which can handle which record ID has been clicked, post back with that value and refresh the collection of data from that (maybe in another repeater). Don't dynamically create buttons and bind events to them otherwise you will end up in a mess.
Hope this helps.

Combobox not showing items? winforms

Can any of you on stackoverflow explain me how come my combobox is not displaying any items. I have a combobox that get values from another so it only shows specific items, in this case the items are seat rows. I have tried to set a breakpoit and see if its null, and its not it gets the right value. But for some reason the combobox dont display the items ? can any of you help me.
Here is the first combobox selectIndex event:
private void cbBookedSeatMovDate_SelectedIndexChanged(object sender, EventArgs e)
{
ServiceReferenceMovieRunTime.MovieRunTimeServiceClient movRunService = new ServiceReferenceMovieRunTime.MovieRunTimeServiceClient();
string _selectedMovName = Convert.ToString(cbBookedSeatMovInfo.SelectedValue);
string _selectedMovDate = Convert.ToString(cbBookedSeatMovDate.SelectedValue);
cbBookedSeatMovTime.DataSource = movRunService.GetRunTimeOnMovNameAndDate(_selectedMovName, _selectedMovDate);
cbBookedSeatMovTime.ValueMember = "id";
cbBookedSeatMovTime.DisplayMember = "startTime";
}
and here is the combobox event thats not working right:
private void cbBookedSeatMovTime_SelectedIndexChanged(object sender, EventArgs e)
{
ServiceReferenceMovieRunTime.MovieRunTimeServiceClient movRunService = new ServiceReferenceMovieRunTime.MovieRunTimeServiceClient();
ServiceReferenceSeats.SeatsServiceClient seatService = new ServiceReferenceSeats.SeatsServiceClient();
string _selectedMovTime = Convert.ToString(cbBookedSeatMovTime.SelectedValue);
string _selectedMovName = Convert.ToString(cbBookedSeatMovInfo.SelectedValue);
string _selectedMovDate = Convert.ToString(cbBookedSeatMovDate.SelectedValue);
string _runTimeId = Convert.ToString(movRunService.GetRunTimeOnNameDateAndTime(_selectedMovName, _selectedMovDate, _selectedMovTime));
cbRow1.DataSource = seatService.GetRowsOnRunTime(Convert.ToInt32(_runTimeId.First()));
cbRow1.ValueMember = "id";
cbRow1.DisplayMember = "rowId";
}
Here is the LINQ query:
public List<TheaterSeat> GetRowsOnRunTime(int RunTime)
{
var queryResult = (from x in db.TheaterSeats
where x.runTime == RunTime
select x);
return queryResult.ToList();
}
Hope you guys can help me out!

Cannot select specific row on Janus GridEX

So I'm accessing a DB through a stored procedure and am able to pull the data I wanted into a popup box. The problem now is that no matter what row I click on, the SAME data keeps populating my fields. How the heck can I choose a particular row, and have that specific row's data pulled from the DB? Thanks for any help
Form1
public partial class DSC_Mon : Form
{
DSCU_SvcConsumer.MTCaller Caller;
DataSet dsZA;
DataSet dsOut;
public DSC_Mon()
{
InitializeComponent();
Caller = new DSCU_SvcConsumer.MTCaller();
dsZA = new DataSet();
dsOut = new DataSet();
}
private void DSC_Mon_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void LoadData()
{
if (!dsZA.Tables.Contains("Query"))
{
DataTable dtZA = dsZA.Tables.Add("Query");
dtZA.Columns.Add("str");
dtZA.Rows.Add(string.Format(#"exec MON_GetStatus"));
}
//dtZA.Rows.Add(string.Format(#"select * from am_company"));
dsOut.Clear();
dsOut.Merge(Caller.CallRequest("ZuluAction", dsZA));
if (gridEXMon.DataSource == null)
{
gridEXMon.DataSource = dsOut;
gridEXMon.DataMember = "Table";
}
//gridEXMon.RetrieveStructure();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
LoadData();
timer1.Enabled = true;
}
private void gridEXMon_DoubleClick(object sender, EventArgs e)
{
ReportInfo report = new ReportInfo();
report.ShowDialog();
}
}
I replaced the private void gridEXMon_DoubleClick with:
private void gridEXMon_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
int rowIndex = Convert.ToInt32(e.Row.RowIndex);
ReportInfo report = new ReportInfo(rowIndex);
report.ShowDialog();
}
Popup Dialog
public partial class ReportInfo : Form
{
public ReportInfo()
{
InitializeComponent();
DSCU_SvcConsumer.MTCaller caller = new DSCU_SvcConsumer.MTCaller();
DataSet dsZA = new DataSet();
DataSet dsOut = new DataSet();
if (!dsZA.Tables.Contains("Query"))
{
DataTable dtZA = dsZA.Tables.Add("Query");
dtZA.Columns.Add("str");
dtZA.Rows.Add(string.Format(#"MON_ReportInfo"));
}
dsOut.Clear();
dsOut.Merge(caller.CallRequest("ZuluAction", dsZA));
DataTable dt = dsOut.Tables["Table"];
DataRow dr = dt.Rows[0];
if (dt != null)
{
systemNameTextBox.Text = dr["System"].ToString();
contactName1TextBox.Text = dr["Manager"].ToString();
functionNameTextBox.Text = dr["Function"].ToString();
durationMSTextBox.Text = dr["Speed"].ToString();
}
}
I then sent the rowIndex to the popup:
DataTable dt = dsOut.Tables["Table"];
DataRow dr = dt.Rows[rowIndex];
systemNameTextBox.Text = dr["System"].ToString();
contactName1TextBox.Text = dr["Manager"].ToString();
functionNameTextBox.Text = dr["Function"].ToString();
durationMSTextBox.Text = dr["Speed"].ToString();
}
Better to get the data from the grid to save the extra call to the database in your ReportInfo class.
Here is the code:
private void gridEXMon_DoubleClick(object sender, EventArgs e)
{
if (e.Row.RowIndex < 0)
return;
ReportInfo report = new ReportInfo();
String System = Convert.ToInt32(e.Row.Cells["System"].Value);
String Manager = Convert.ToString(e.Row.Cells["Manager"].Value);
String Function = Convert.ToDecimal(e.Row.Cells["Function"].Value);
String Speed = Convert.ToInt32(e.Row.Cells["Speed"].Value);
report.ShowDialog(System, Manager, Function, Speed);
}
Then your ReportInfo class ctor should be:
public partial class ReportInfo : Form
{
public ReportInfo(String System, String Manager, String Function, String Speed)
{
InitializeComponent();
systemNameTextBox.Text = System;
contactName1TextBox.Text = Manager;
functionNameTextBox.Text = Function;
durationMSTextBox.Text = Speed;
}
}
Have you tried with:
gridEXMon.DataSource = dsOut.Tables[0];
// next line is not required if you've already defined proper grid structure
gridEXMon.RetrieveStructure();
Janus has also his own forum available here. You can find there a lot of useful information. For browsing I will use IE. Janus forum works good only with IE...
Edited:
How ReportInfo class can know what record have you selected? Especially that you have fallowing code:
DataTable dt = dsOut.Tables["Table"];
DataRow dr = dt.Rows[0]; // always first record is taken!
Probably you should define ctor with row index:
public ReportInfo(int rowIndex)
{
...
DataTable dt = dsOut.Tables["Table"];
DataRow dr = dt.Rows[rowIndex];
....
}

ListBox SelectValue is null and selectindex = -1

The listbox populates but can't find the selected value. It's either null or the default value (first item). Whenever I select another item, it changes to null. I did the !ispostback but still nothing. Using a wizard in asp.net/c#/sql. Any help is appreciated.
protected void Page_Load(object sender, EventArgs e)
{
ListBox lstService = (ListBox)Wizard1.FindControl("lstService");
string s = lstService.SelectedValue.ToString();
int s1 = lstService.SelectedIndex;
if (s == "MarketTrack Toys")
{
Wizard2.Visible = true;
}
if (!Page.IsPostBack)
{
BindGrid();
}
if ((Wizard1.ActiveStepIndex <= 5) && (Wizard1.ActiveStepIndex != 0))
{
Wizard1.DisplaySideBar = true;
Wizard2.DisplaySideBar = false;
}
else
{
Wizard1.DisplaySideBar = false;
Wizard2.DisplaySideBar = true;
}
}
private void BindGrid()
{
dAS = new DataAccessClass();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds = dAS.func_FillDataset("select servicename from dbo.Services", "Services");
ListBox lstService = (ListBox)Wizard1.FindControl("lstService");
lstService.DataSource = ds;
lstService.DataTextField = "ServiceName";
lstService.DataValueField = "ServiceName";
lstService.DataBind();
if (lstService.Items.Count > 0)
{
lstService.SelectedIndex = 0;
}
}
Walk it through in a debugger.
Whatever Object that is being placed in the ListBox doesn't know to convert itself to the string you're looking for with ToString()
The ListBox populates correctly because you specify how to get the names with lstService.DataTextField = "ServiceName";
You may have to rework the object you're putting in the ListBox or just override to ToString for that object instead.

How to add item to Repeater control manually

First of all:
_ddlOptions is drop down list
_selectedOptions is repeater control
and it's just provisional code of my final control.
What I want to do is to get data for _ddlOption on !IsPostBack. There is Add button that enables user to move selected drop down item to repeater control.
It the following way of updating Repeater.Items correct? I found many solution of adding/removing elements manually using DataSource, but here my DataSource is null, as I set it only on !IsPostBack.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_ddlOptions.DataSource = new[] { 1, 2, 3 };
_ddlOptions.DataBind();
}
}
protected void OnAdd(object sender, EventArgs e)
{
var list = new ArrayList(_selectedOptions.Items);
list.Add(_ddlOptions.SelectedItem);
_ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);
_selectedOptions.DataSource = list;
_selectedOptions.DataBind();
}
If you only need to fetch data once and you're going to use viewstate, get the data first time you need it, store it in VS and get it from VS for all future PostBacks.
Example:
public List<int> Data
{
get
{
if (ViewState["Data"] == null)
{
// Get your data, save it and return it.
var data = new List<int> { 1, 2, 3 };
ViewState["Data"] = data;
return data;
}
return (List<int>)ViewState["Data"];
}
set
{
ViewState["Data"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData(Data);
}
}
private void BindData(List<int> data)
{
_ddlOptions.DataSource = data;
_ddlOptions.DataBind();
}
protected void OnAdd(object sender, EventArgs e)
{
var existing = Data;
existing.Add(_ddlOptions.SelectedItem);
_ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);
Data = existing;
BindData(existing);
}
I didn't test this - and its only my first thought but you can build on it from here.
Patrick.
Looks good to me. You just may want to move the decalration for your list outside the onAdd method. As you have it I think it will be reinitialized every time the add button is clicked, so you'll never have more than the currently selected item in your repeater.
You can use a DataAdapter to fill a table in a DataSet.
DataSet ds = new DataSet();
using (SqlConnection conn = YourConnectionFactory.GetConnection())
{
SqlCommand objComm = DBHelper.CreateStoredProc("YourStoredProcedure",
conn);
SqlDataAdapter adapt = new SqlDataAdapter(objComm);
adapt.Fill(ds, TableName);
conn.Close();
}
DataTable dt = ds.Tables[0];
for (int a=dt.Rows.Count-1; a>= 0; a--)
{
// check and insert as necessary
}
YourControl.DataSource = ds;
YourControl.DataBind();
You can also do something like this then,
like rebind Taken from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx:
Dim values As New ArrayList()
values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values Repeater1.DataBind()
Repeater2.DataSource = values Repeater2.DataBind()

Categories

Resources