I am so confused right now. I have multiple methods that binds gridview from sql connection.
When I call those from a dropdown index change, it runs fine.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int machineid = int.Parse(DropDownList1.SelectedValue);
try {
connection.Open();
EquimentBind(machineid);
}
finally { connection.Close(); }
}
But when I call the methods from page_load and pass the id from URL it always give me StackOverflowException on the adapter.fill(dataTable) step.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string station_id = Request.QueryString["station_id"];
if (station_id != null)
{
DropDownList1.Visible = false;
int machineid = int.Parse(station_id);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
EquimentBind(machineid);
}
finally { connection.Close(); }
}
else
{
try
{
if (connection.State != ConnectionState.Open) { connection.Open(); }
SqlDataAdapter adapter = //My sql command
DataTable stalist = new DataTable();
adapter.Fill(stalist);
DropDownList1.DataSource = stalist;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
finally { connection.Close(); }
DropDownList1.SelectedValue = Convert.ToString('0');
}
}
Below is my data binding method:
protected void EquimentBind(int machineid)
{
SqlCommand Sqlcommand = // my sql command
SqlCommand command = Sqlcommand;
SqlDataAdapter sd = new SqlDataAdapter(command);
DataTable dt1 = new DataTable();
sd.Fill(dt1);
GV_Equipments.DataSource = dt1;
GV_Equipments.DataBind();
command.ExecuteNonQuery();
dt1.Clear();
}
The StackOverflowExecption always throw at sd.Fill(dt1) row.
What am I doing wrong?
1条答案
按热度按时间qqrboqgw1#
The StackOverflowException is likely occurring because the EquimentBind method is being recursively called in an infinite loop. This can happen when the Page_Load event is triggered repeatedly due to the postback caused by the data binding process.
Remove the data binding logic from the Page_Load event when station_id is null, as it is unnecessary and causing the recursive calls. You can simplify the Page_Load event as follows:
Modify the DropDownList1_SelectedIndexChanged event to handle both cases, i.e., when the selection is changed from the dropdown and when it is loaded from the URL: