我为一个大学项目创建了一个ASP.NET Web应用程序,我使用本地mysql db完成所有功能。由于我无法控制的原因,我被指示将数据库更改为访问数据库。所以我做了这个,改掉了“sql' Oledb'的陈述我的应用程序的一半和一半与新的更改工作正常,但我有另一半的问题。我将从我的web.config页面添加我的连接字符串,然后是我遇到的问题。有人能看出我出错的地方吗?
<add name="newregDBConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Alex\Documents\Database1.accdb"
providerName="System.Data.OleDb" />
我有两个问题,第一个是我有一个注册页面,你填写一个表格,细节存储在数据库中。当我单击“提交”将详细信息发送到数据库时,出现错误;
第二个问题是我有一个使用数据源的gridview显示注册的人,但是当我点击视图按钮时,我收到以下错误;
我添加了
&#34;扔新的 例外(ConfigurationManager.ConnectionStrings [&#34; newregDBConnectionString&#34]。ConnectionString中);&#34; 到我的global.aspx页面查看错误,它显示以下内容;
它使我的web.config页面中的连接字符串看起来不正确,但我的应用程序的另一半正在使用数据库连接(我的登录页面),所以我无法看到问题。对于长篇帖子道歉,我非常感谢有人指出我愚蠢的地方!提前谢谢。
更新:
protected void submitBtn_Click(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
{
if (parentRadBtn.Checked)
{
if (firstNameBox.Text == "" || surnameBox.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
else
{
OleDbCommand pa = new OleDbCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
pa.Parameters.AddWithValue("@parentID", userBox.Text);
pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
pa.Parameters.AddWithValue("@surname", surnameBox.Text);
pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
pa.Parameters.AddWithValue("@telephone", teleBox.Text);
pa.Parameters.AddWithValue("@email", emailBox.Text);
pa.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
pa.ExecuteNonQuery();
connect.Close();
}
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
}
else if (childRadBtn.Checked)
{
if (firstNameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
else
{
OleDbParameter dob = new OleDbParameter("@dob", System.Data.SqlDbType.DateTime);
dob.Value = new DateTime(Int32.Parse(yearDobList.Text), Int32.Parse(monthDobList.Text), Int32.Parse(dayDobList.Text));
OleDbCommand ca = new OleDbCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
ca.Parameters.AddWithValue("@childID", userBox.Text);
ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
ca.Parameters.Add(dob);
ca.Parameters.AddWithValue("@gender", genderList.Text);
ca.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
ca.ExecuteNonQuery();
connect.Close();
}
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
}
}
}
答案 0 :(得分:0)
看看这个:
OleDbConnection connect = new OleDbConnection("newregDBConnectionString");
请注意,newregDBConnectionString
在引号中?它看起来像是保存连接字符串的变量的名称。通过将其放在引号中,您将文本“newregDBConnectionString
”作为连接字符串本身传递,而不是该变量中的任何字符串。
您需要从配置管理器获取此连接字符串值,就像在第二张和第三张图像中一样。
此外,在第二张图片中,您有:
SqlDataSource source = new SqlDataSource();
这需要更改为OleDbDataSource
。
答案 1 :(得分:0)
您的登录页面有此
OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
在您的注册表单页面上,我认为您只是传递一个字符串
使其与登录页面相同