我正在构建ASP.NET webforms中的库管理应用程序,我有一个表单应该将新用户添加到数据库中。但是当我尝试提交它时,页面只会闪烁,并且没有任何内容添加到数据库中。
这是aspx代码:
<div style="margin-top: 20px;">
<form class="form-horizontal">
<fieldset>
<legend>Create new User</legend>
<div class="form-group">
<label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
<div class="col-lg-10">
<asp:TextBox class="form-control" ID="inputFirstName" placeholder="First Name" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
<div class="col-lg-10">
<asp:TextBox class="form-control" ID="inputLastName" placeholder="Last Name" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Adress</label>
<div class="col-lg-10">
<asp:TextBox class="form-control" ID="inputStreet" placeholder="Street" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
<div class="col-lg-10 col-lg-offset-2">
<asp:TextBox class="form-control" ID="inputCity" placeholder="City" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
<div class="col-lg-10 col-lg-offset-2">
<asp:TextBox class="form-control" ID="inputCountry" placeholder="Country" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label for="inputLastName" class="col-lg-2 control-label">Phone Number</label>
<div class="col-lg-10">
<asp:TextBox class="form-control" ID="inputPhoneNumber" placeholder="Phone Number" Style="margin-bottom: 10px;" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<asp:LinkButton runat="server" Text="Cancel" class="btn btn-default" />
<asp:LinkButton runat="server" Text="Submit" class="btn btn-primary" OnClik="Submit" />
</div>
</div>
</fieldset>
</form>
</div>
这是背后的代码:
private void Submit(object sender, EventArgs e)
{
string FirstName = inputFirstName.Text;
string LastName = inputLastName.Text;
string Street = inputStreet.Text;
string City = inputCity.Text;
string Country = inputCountry.Text;
string PhoneNumber = inputPhoneNumber.Text;
using (SqlConnection c = new SqlConnection("{LibraryContext}"))
{
c.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO LibUsers(UserFirstName,UserLastName,Adress,PhoneNumber) VALUES (@Fname, @Lname,@Adress,@PhoneNumber)"))
{
cmd.Parameters.AddWithValue("@Fname", FirstName);
cmd.Parameters.AddWithValue("@Lname", LastName);
//i know concatenation like this is bad
cmd.Parameters.AddWithValue("@Adress", Street + " " + City + " " + Country);
cmd.Parameters.AddWithValue("@PhoneNumber", PhoneNumber);
cmd.ExecuteNonQuery();
}
}
}
任何人都可以帮我解决这个问题吗?
提前致谢。
答案 0 :(得分:4)
将OnClik="Submit"
更改为OnClick="Submit"
。
答案 1 :(得分:1)
给出web.config文件中的连接字符串:
<connectionStrings>
<add name="PubsConnectionString" connectionString="Server=(local);Database=pubs;trusted_connection=yes"/>
</connectionStrings>
获取值,然后将其传递给SqlConnection构造函数。
string connectionString = ConfigurationManager.ConnectionStrings["PubsConnectionString"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{ ...