When I select any value from the DropDownList, the first one gets selected.
The code's purpose is whenever I choose a value from the DropDownList the corresponding value from the database should be displayed in the TextBox.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable Seminars = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT SeminarName, ID FROM SeminarData", con);
adapter.Fill(Seminars);
DropDownList1.DataSource = Seminars;
DropDownList1.DataTextField = "SeminarName";
DropDownList1.DataValueField = "SeminarName";
DropDownList1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable dt = new DataTable();
SqlCommand sqlCmd = new SqlCommand("SELECT SeminarNameE,TrainerName FROM SeminarData WHERE SeminarName='" + DropDownList1.SelectedItem.Value +"'", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0]["SeminarNameE"].ToString();
TextBox2.Text = dt.Rows[0]["TrainerName"].ToString();
}
}
Replace your page load with this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable Seminars = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT SeminarName, ID FROM SeminarData", con);
adapter.Fill(Seminars);
DropDownList1.DataSource = Seminars;
DropDownList1.DataTextField = "SeminarName";
DropDownList1.DataValueField = "SeminarName";
DropDownList1.DataBind();
con.Close();
}
}
It needs !Page.IsPostBack, because everytime you bind, you clear any selections. In this code, it binds on every page load. Adding !Page.IsPostback will ensure it binds only the first load.
Related
I want to take the drop down list's selected value to update another drop down list. data binding is working. but the "ddl1"'s selected value not change when selecting an item. therefore "ddl1.SelectedValue" notworkin correctly
Please check this code & help me to correct it
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();
string selectmap = "select [id]+' - '+[title] as title, id from map order by id;";
SqlDataAdapter comm = new SqlDataAdapter(selectmap, conn);
DataTable dt = new DataTable();
comm.Fill(dt);
conn.Close();
ddl1.DataSource = dt;
ddl1.DataTextField = "title";
ddl1.DataValueField = "id";
ddl1.DataBind();
ddl1.Items.Insert(0, new ListItem("---- Select Map ----", "0"));
Label2.Text = ddl1.SelectedIndex.ToString();
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();
string selectmap2 = "select [id]+' - '+[title] as title, id from map where id !='"+ddl1.SelectedValue+"' order by id;";
SqlDataAdapter comm2 = new SqlDataAdapter(selectmap2, conn);
DataTable dt2 = new DataTable();
comm2.Fill(dt2);
ddl2.DataSource = dt2;
ddl2.DataTextField = "title";
ddl2.DataValueField = "id";
ddl2.DataBind();
ddl2.Items.Insert(0, new ListItem("---- Select Map ----", "0"));
}
ASPX:
<asp:DropDownList ID="ddl1" runat="server" CssClass="form-control" AutoPostBack="True" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Populateddl1();
}
}
private void Populateddl1()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();
string selectmap = "select [id]+' - '+[title] as title, id from map order by id;";
SqlDataAdapter comm = new SqlDataAdapter(selectmap, conn);
DataTable dt = new DataTable();
comm.Fill(dt);
conn.Close();
ddl1.DataSource = dt;
ddl1.DataTextField = "title";
ddl1.DataValueField = "id";
ddl1.DataBind();
ddl1.Items.Insert(0, new ListItem("---- Select Map ----", "0"));
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
//Write here for populating second dropdown code
}
i am facing a problem do not know how to set this problem the problem is that my project working fine but when i delete all the rows from sql databse its not show grid kindly help
your response will be highly appreciated
Here is my Code Behind
public partial class Web_grid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
SqlConnection conne = new SqlConnection("Data Source=192.168.0.6;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=malick");
DataSet ds = new DataSet();
conne.Open();
string cmdstr = "SELECT * FROM OPR1 ";
SqlCommand cmd = new SqlCommand(cmdstr, conne);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
cmd.ExecuteNonQuery();
conne.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
// GridView1.DataSource =null;
// GridView1.DataSource = ds;
// GridView1.DataBind();
//ds = null;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection conne = new SqlConnection("Data Source=192.168.0.6;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=malick");
conne.Open();
if (e.CommandName.Equals("ADD"))
{
Calendar txtOpenDate = (Calendar)GridView1.FooterRow.FindControl("txtOpenDate");
TextBox txtCloseDate = (TextBox)GridView1.FooterRow.FindControl("txtCloseDate");
DropDownList DropDownListoppr = (DropDownList)GridView1.FooterRow.FindControl("DropDownListoppr");
DropDownList DropDownListStages = (DropDownList)GridView1.FooterRow.FindControl("DropDownListStages");
TextBox txtAddLine = (TextBox)GridView1.FooterRow.FindControl("txtAddLine");
TextBox txtStages = (TextBox)GridView1.FooterRow.FindControl("txtStages");
TextBox txtAddOppId = (TextBox)GridView1.FooterRow.FindControl("txtAddOppId");
string cmdstr = "insert into OPR1(OpenDate,CloseDate,SlpCode,Step_Id,Line,OpprId) values(#txtOpenDate,#txtCloseDate,#SlpCode,#Step_Id,#txtAddLine,#txtAddOppId)";
SqlCommand cmd = new SqlCommand(cmdstr, conne);
cmd.Parameters.AddWithValue("#txtOpenDate", txtOpenDate.TodaysDate);
cmd.Parameters.AddWithValue("#txtCloseDate", txtCloseDate.Text);
cmd.Parameters.AddWithValue("#Step_Id", DropDownListStages.SelectedValue.ToString()); // SelectedItem.ToString());
cmd.Parameters.AddWithValue("#SlpCode", DropDownListoppr.SelectedValue.ToString()); // SelectedItem.ToString());
cmd.Parameters.AddWithValue("#txtStages", txtStages.Text);
cmd.Parameters.AddWithValue("#txtAddLine", txtAddLine.Text);
cmd.Parameters.AddWithValue("#txtAddOppId", txtAddOppId.Text);
cmd.ExecuteNonQuery();
// this.TextBox1.Text = DropDownList1.SelectedItem.ToString();
// this.TextBox3.Text = DropDownList1.SelectedValue.ToString();
BindData();
conne.Close();
}
}
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList DropDownListoppr = (DropDownList)e.Row.FindControl("DropDownListoppr");
DropDownList DropDownListStages = (DropDownList)e.Row.FindControl("DropDownListStages");
DataTable CardCode = new DataTable();
DataTable CardCode1 = new DataTable();
SqlConnection connection = new SqlConnection("Data Source=192.168.0.6;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=malick");
using (connection)
{
SqlCommand theCommand = new SqlCommand("select SlpCode,SlpName from OSLP ", connection);
SqlCommand theCommand1 = new SqlCommand("select Distinct StepId, Descript from OOST ", connection);
SqlDataAdapter adapter = new SqlDataAdapter(theCommand);
SqlDataAdapter adapter1 = new SqlDataAdapter(theCommand1);
adapter.Fill(CardCode);
adapter1.Fill(CardCode1);
//DropDownList7.DataSource = CardCode;
//DropDownList7.DataTextField = "SlpName";
//DropDownList7.DataValueField = "SlpCode";
//DropDownList7.DataBind();
if (CardCode.Rows.Count > 0)
{
for (int i = 0; i < CardCode.Rows.Count; i++)
{
string name3 = CardCode.Rows[i]["SlpName"].ToString();
string slpCode = CardCode.Rows[i]["SlpCode"].ToString();
DropDownListoppr.Items.Add(new ListItem(name3, slpCode));
}
}
if (CardCode1.Rows.Count > 0)
{
for (int j = 0; j < CardCode1.Rows.Count; j++)
{
string name4 = CardCode1.Rows[j]["Descript"].ToString();
string stageCode = CardCode1.Rows[j]["StepId"].ToString();
DropDownListStages.Items.Add(new ListItem(name4, stageCode));
}
}
}
}
}
No need of cmd.ExecuteNonQuery(); in BindData Method, As you are not performing any insert,delete or update operation.
try to add this property to your aspx gridview
EmptyDataText="someText"
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatext(v=vs.110).aspx
or you can use EmptyDataTemplate - just like TemplateField
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate(v=vs.110).aspx
I am trying to change value of a TextBox control after the value of ComboBox changes. My code returns System.Data.DataRowView on the first time ComboBox value changes.
That is why I am getting an Object instead of the actual value on first call to my combobox.
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionString"].ConnectionString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd= new SqlCommand("SELECT donor_name,ID FROM donor_detail",con);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
DonorName.DataSource = dt;
DonorName.DisplayMember = "donor_name";
DonorName.ValueMember = "ID";
con.Close();
}
And this is Event of change Value of ComboBox which is supposed to change value of my TextBox also when value in ComboBox changes.
private void DonorName_SelectionChangeCommitted(object sender, EventArgs e)
{
var id = Convert.ToString(DonorName.SelectedValue);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionString"].ConnectionString);
con.Open();
String sql = "SELECT * from donor_detail WHERE ID=" + id + "";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show(sql);
mobile.Text = dt.Rows[0]["mobile"].ToString();
con.Close();
}
As mentioned here and several other places, this happens if you don't specify the datasource after the display and value members. This is the correct order:
DonorName.DisplayMember = "donor_name";
DonorName.ValueMember = "ID";
DonorName.DataSource = dt;
I want to fill datagridview when rows affected=< 0 but i get error in line 47 gridview (id) = "correctgridview" runat = "server"
the first case work well but the error in the datagridview
mycode
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void insertButton_Click(object sender, EventArgs e)
{
int rowsaffected = 0;
dataset ds = new dataset();
SqlConnection cn = new SqlConnection(#"Server=.\SQLEXPRESS; DataBase=attend; Integrated Security=true;");
SqlCommand cmd;
cn.Open();
cmd = new SqlCommand("atten", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param.ParameterName = "#myid";
param.Value = idTextBox.Text;
cmd.Parameters.Add(param);
rowsaffected = cmd.ExecuteNonQuery();
if (rowsaffected > 0)
{
resultLabel.Text = "added successful";
}
else
{
resultLabel.Text = "attend error";
SqlCommand command;
command = new SqlCommand("select id, courses.course , course_date , st_lec , location from courses inner join regestration on courses.course = regestration.course where id = 2", cn);
SqlDataAdapter adapte = new SqlDataAdapter(command);
DataTable Dt = new DataTable();
adapte.Fill(Dt);
correctGridView.DataSource = Dt;
correctGridView.DataBind();
}
cn.Close();
}
}
So here I'm trying to populate my dropdown list, the code behind is as below:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
con.Open();
SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
SqlDataReader ddlvalues = mycommand.ExecuteReader();
ddlTransactionCategory.DataSource = ddlvalues;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
mycommand.Connection.Close();
mycommand.Connection.Dispose();
}
the problem is, I can't seem to get it to work, any help? and is this code doing it right?
plz try below code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
con.Open();
SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
SqlDataAdapter adp =new SqlDataAdapter(mycommand);
DataSet ds =new DataSet();
adp.Fill(ds);
ddlTransactionCategory.DataSource = ds;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
mycommand.Connection.Close();
mycommand.Connection.Dispose();
}
Thanks,
Hitesh
Can't bind to a SqlDataReader (or at least I've never tried it). Get a DataTable or a DataSet instead, fill it and then bind that to the dropdown the same way.
Use SqlDataAdataper like the one below
using (SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from MSUnit", con);
DataTable dt = new DataTable
da.Fill(dt)
ddlTransactionCategory.DataSource = dt;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
}
if you want to use DataReader you must insert one-by-one.
while (ddlvalues.Read())
{
ddlTransactionCategory.Items.Add(new ListItem(ddlvalues.getString("OrgID"),ddlvalues.getString("categoryCode")))
}