我不知道如何插入以访问数据库输入类型=“ date”

时间:2018-10-19 08:12:09

标签: c# html asp.net database

我有一个网站,我做了asp:button并在数据库中添加了asp:textbox,它起作用了。 但是,当我在上尝试插入时,它对我不起作用。我正在尝试在aspx.cs中将插入date.text写到我的数据库中,并找到它的ID,因为它是输入而不是asp:textbox。

<td class="style4">
    תאריך לידה:
</td>
<td class="style3">
    <input id="date" type="Date" min="1905-01-01" max="2019-01-01" value="2018-01-01"/>
</td>

OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WebSites\MyWebsite\DataBase.mdb;Persist Security Info=False;";
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into users (FirstName, LastName, Place, Email, UserName, Birthday) values('" + firstname.Text + "','" + lastname.Text + "','" + PlaceList.Text + "','" + MyEmail.Text + "','" + AccountText.Text + "','" + date.Text + "')";
command.ExecuteNonQuery();

我得到一个错误:

  

错误29当前的名称'date'不存在   上下文F:\ WebSites \ MyWebsite \ Register.aspx.cs 27242 F:\ WebSites \ MyWebsite \

找不到此ID。

请,有人可以帮我解决吗? :(

3 个答案:

答案 0 :(得分:1)

您必须指定runat属性,以使元素可编程并可以从背后的代码访问:

<input id="date" type="date" runat="server" />

此外,您应该从不从不直接在查询中将用户输入解析为参数,因为它不可避免地会导致您遇到security和其他问题(例如字符串中的引号,无效非数字值,基于用户区域设置的日期格式等)。请改用command parameters

OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WebSites\MyWebsite\DataBase.mdb;Persist Security Info=False;";

try
{
    connection.Open();

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "insert into users (FirstName, LastName, Place, Email, UserName, Birthday) values(@firstName, @lastName, @placeList, @myMail, @accountText, @date)";
    command.Parameters.AddWithValue("@firstName", firstName.Text);
    command.Parameters.AddWithValue("@lastName", lastName.Text);
    command.Parameters.AddWithValue("@placeList", placeList.Text);
    command.Parameters.AddWithValue("@myMail", myMail.Text);
    command.Parameters.AddWithValue("@accountText", accountText.Text);
    command.Parameters.AddWithValue("@date", date.Text);

    command.ExecuteNonQuery();
}
/* handle various exceptions */
catch (Exception ex)
{
    /*...*/
}
finally
{
    connection.Close();
}

答案 1 :(得分:0)

您需要使其runat = "server" 立即尝试!

 <input id="date" type="Date" min="1905-01-01" max="2019-01-01" value="2018-01-01" runat = "server"/>

 OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WebSites\MyWebsite\DataBase.mdb;Persist Security Info=False;";
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    connection.Open();
    command.CommandText = "insert into users (FirstName, LastName, Place, Email, UserName, Birthday) values('" + firstname.Text + "','" + lastname.Text + "','" + PlaceList.Text + "','" + MyEmail.Text + "','" + AccountText.Text + "','" + date.Text + "')";
    command.ExecuteNonQuery();

答案 2 :(得分:0)

在后面的代码中,您可以通过名称而不是通过id获得表单元素,因此请为输入元素命名:

<input id="date" name = "date" type="Date" min="1905-01-01" max="2019-01-01" value="2018-01-01"/>
                 ^^^^^^^^^^^^

然后可以通过Request.Form["date"]访问它:

command.CommandText = "insert into users (FirstName, LastName, Place, Email, UserName, Birthday) values('" + firstname.Text + "','" + lastname.Text + "','" + PlaceList.Text + "','" + MyEmail.Text + "','" + AccountText.Text + "','" + Request.Form["date"] + "')";

还要注意,此输入必须在表单标记内。