I have a panel that is visible only when the Edit button from GridView
is clicked.
In that panel is a form with a DropDownList
and a TextBox
where you can write a number and add it to a ListBox
.
After all the numbers wanted are added to the ListBox
when I click the button Finalize is adding to the database the data. In Gridview
I have the name concatenate from database , because I have Lastname and Firstname separately.
To be more easy I chose to add from database in DropDownList
when I click on Edit button with the specific ID.
When Edit button is clicked this error is showing:
Operation is not valid due to the current state of the object.
In the line where I add in DropDownList
. I verified the names of my database, of my table , all and is correct. I even tried only with firstname , not concatenate and it does the same error. I don't know what is wrong. I hope you can help me. This is the code where the error appears.
protected void btnEditSO_Click(object sender, EventArgs e)
{
panelSO.Visible = true;
btnFinalizeSO.Text = " Update ";
Button but = (Button)sender;
GridViewRow grid = (GridViewRow)but.NamingContainer;
string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME FROM REFERENT_SHIPPING WHERE ID=" + grid.Cells[14].Text;
using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
{
con.Open();
OracleCommand cmd1 = new OracleCommand(select_sql_SOddl, con);
OracleDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
ddlReferentShip.Items.Add(dr[0].ToString());
// ddlReferentShip.Items.Add(dr["REFERENTNAME"].ToString());
// ddlReferentShip.DataSource = dr1;
// ddlReferentShip.DataTextField = dr1["REFERENTNAME"].ToString();
// ddlReferentShip.DataValueField = dr1["ID"].ToString();
// ddlReferentShip.DataBind();
}
}
}
You are checking dr1.Read()
and reading dr[0].Tostring()
Also try to clear the list before adding the data. Then the index should be 1 if you need to show the name
it should be
while (dr1.Read())
{
ddlReferentShip.Items.Add(dr1[1].ToString());
}
I guess ID is numeric in query you are passing as .Text
try this
string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME
FROM REFERENT_SHIPPING WHERE ID=" + Convert.ToInt32(grid.Cells[14].Text);
also all ways try to use parameterized query to avoide SQL INJECTION
Why you set ddl item from dr
when you have dr1
as datareader.
Maybe you can try this:
protected void btnEditSO_Click(object sender, EventArgs e)
{
panelSO.Visible = true;
btnFinalizeSO.Text = " Update ";
Button but = (Button)sender;
GridViewRow grid = (GridViewRow)but.NamingContainer;
string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME FROM REFERENT_SHIPPING WHERE ID=" + grid.Cells[14].Text;
using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
{
con.Open();
OracleCommand cmd = new OracleCommand(select_sql_SO, con);
OracleCommand cmd1 = new OracleCommand(select_sql_SOddl, con);
OracleDataReader dr = cmd.ExecuteReader();
OracleDataReader dr1 = cmd1.ExecuteReader();
int i=0;
while (dr1.Read())
{
ddlReferentShip.Items.Add(dr1[1].ToString());
i++;
// ddlReferentShip.Items.Add(dr["REFERENTNAME"].ToString());
// ddlReferentShip.DataSource = dr1;
// ddlReferentShip.DataTextField = dr1["REFERENTNAME"].ToString();
// ddlReferentShip.DataValueField = dr1["ID"].ToString();
// ddlReferentShip.DataBind();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.