【发布时间】:2013-11-12 11:14:50
【问题描述】:
您好,谁能帮助我不知道为什么我的按钮中的计数器无法正常工作,对所有其他代码感到抱歉。我想通过使用计数器来构建数据库中的数据。如果有更简单的方法也可以。
public partial class _Default : System.Web.UI.Page
{
int iDefualt = 2;
int iMainCounter = 0;
SqlConnection con1 = new SqlConnection("Data Source=EON;Initial Catalog=DW2;Persist Security Info=True;User ID=Kapow;Password=Kapow");
DataTable dt = new DataTable();
public void Page_Load(object sender, EventArgs e)
{
con1.Open();
SqlDataReader myReader = null;
//SqlCommand myCommand = new SqlCommand("select * from customer_registration where username='" + Session["username"] + "'", con1);
SqlCommand myCommand = new SqlCommand(sNavigate(0), con1);
/SELECT * FROM tblDW2 WHERE [User]='Petrus'
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
txtbxDaywords.Text = (myReader["Dayword"].ToString());
}
con1.Close();
iMainCounter = iDefualt;
// "Daywords\t" + "\n" + DateTime.Now.ToString();
}
public string sNavigate(int iNavNum)
{
int iNavigate;
if (iNavNum != 0)
{
iNavigate = iNavNum;
}
else
{
iNavigate = iDefualt;
}
return "SELECT * FROM (SELECT Dayword, ROW_NUMBER() OVER (ORDER BY Dayword) AS Rownumber FROM tblDW2 WHERE [User]='Petrus' ) results WHERE results.Rownumber = "+ iNavigate.ToString();
}
protected void btnNext_Click1(object sender, EventArgs e)
{
iMainCounter++;
con1.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(sNavigate(iMainCounter), con1);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
txtbxDaywords.Text = (myReader["Dayword"].ToString());
}
con1.Close();
}
}
【问题讨论】:
-
尝试给他 private const int 类型
-
您应该尝试确定您的问题。准备一个只有按钮和计数器的“最小可编译示例”。
-
private const int 不行,它的 const,使用 private static int
-
进一步研究 asp.net 的工作原理