我有这个表单,当我点击保存客户时添加记录
将添加到数据库中并在gridview上显示添加记录
gridview连接在数据库上。 (没有人
gridview的问题。)
这是我的问题..
单击按钮后,我无法在数据库中添加记录
我认为问题在于背后的代码。
任何人都可以帮我解决我的代码有什么问题吗?
的Default.aspx
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
<Scripts>
<%--Needed for JavaScript IntelliSense in VS2010--%>
<%--For VS2008 replace RadScriptManager with ScriptManager--%>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
</Scripts>
</telerik:RadScriptManager>
<script type="text/javascript">
//Put your JavaScript code here.
</script>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
</telerik:RadAjaxManager>
<div>
<div id="padding-left">
<br />
<div id="border">
<div style="float: none; height: 30px; padding-bottom: 3px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Customer ID:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtCustomerID" runat="server" Enabled="false" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">First Name:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtFirstName" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Middle Name:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtMiddleName" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Last Name:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtLastName" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Address:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtAddress" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Contact Number:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtContactNumber" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Birthday:</div>
<div style="float: none;">
<telerik:RadDatePicker ID="dpBirthday" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Age:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtAge" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: left; width: 100px; padding-top: 3px;">Gender:</div>
<div style="float: none;">
<telerik:RadTextBox ID="txtGender" runat="server" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel">
<asp:Label runat="server" ID="InfoMessage" />
</div>
</div>
<div style="float: none; height: 30px;">
<div class="padLabel" style="float: none;">
<telerik:RadButton ID="btnSaveCustomer" runat="server" Text="Add Customer" OnClick="btnSaveCustomer_Click" />
</div>
</div>
</div>
</div>
<div id="padding-innerleft">
<br />
<div align ="right">
<telerik:RadButton runat="server" ID="btnLogut" Text="Logout" OnClick="btnLogut_Click" />
</div>
<br />
<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
GridLines="None">
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="CustomerID" DataType="System.Int32" FilterControlAltText="Filter CustomerID column"
HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" UniqueName="CustomerID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="FirstName" FilterControlAltText="Filter FirstName column"
HeaderText="FirstName" SortExpression="FirstName" UniqueName="FirstName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="MiddleName" FilterControlAltText="Filter MiddleName column"
HeaderText="MiddleName" SortExpression="MiddleName" UniqueName="MiddleName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastName" FilterControlAltText="Filter LastName column"
HeaderText="LastName" SortExpression="LastName" UniqueName="LastName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Address" FilterControlAltText="Filter Address column"
HeaderText="Address" SortExpression="Address" UniqueName="Address">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Birthday" DataType="System.DateTime" FilterControlAltText="Filter Birthday column"
HeaderText="Birthday" SortExpression="Birthday" UniqueName="Birthday">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Age" DataType="System.Int32" FilterControlAltText="Filter Age column"
HeaderText="Age" SortExpression="Age" UniqueName="Age">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Gender" FilterControlAltText="Filter Gender column"
HeaderText="Gender" SortExpression="Gender" UniqueName="Gender">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContactNumber" FilterControlAltText="Filter ContactNumber column"
HeaderText="ContactNumber" SortExpression="ContactNumber" UniqueName="ContactNumber">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableImageSprites="False">
</FilterMenu>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
SelectCommand="SELECT [CustomerID], [FirstName], [MiddleName], [LastName], [Address], [Birthday], [Age], [Gender], [ContactNumber] FROM [v.NewCustomer]">
</asp:SqlDataSource>
</div>
</div>
</form>
</body>
后面的代码
public partial class Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void btnSaveCustomer_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Insert into v.NewCustomer(FirstName, MiddleName, LastName, Address, Birthday, Age, Gender, ContactNumber) values (@FirstName, @MiddleName, @LastName, @Address, @Birthday, @Age, @Gender, @ContactNumber)";
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@MiddleName", txtMiddleName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Birthday", dpBirthday.SelectedDate);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Gender", txtGender.Text);
cmd.Parameters.AddWithValue("@ContactNumber", txtContactNumber.Text);
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
//SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
//SqlCommand com = new SqlCommand("NewCustomer", conn);
//com.CommandType = CommandType.StoredProcedure;
//SqlParameter p1 = new SqlParameter("@FirstName", txtFirstName.Text);
//SqlParameter p2 = new SqlParameter("@MiddleName", txtMiddleName.Text);
//SqlParameter p3 = new SqlParameter("@LastName", txtLastName.Text);
//SqlParameter p4 = new SqlParameter("@Address", txtAddress.Text);
//SqlParameter p5 = new SqlParameter("@Birthday", dpBirthday.SelectedDate);
//SqlParameter p6 = new SqlParameter("@Age", txtAge.Text);
//SqlParameter p7 = new SqlParameter("@Gender", txtGender.Text);
//SqlParameter p8 = new SqlParameter("@ContactNumber", txtContactNumber.Text);
//com.Parameters.Add(p1);
//com.Parameters.Add(p2);
//com.Parameters.Add(p3);
//com.Parameters.Add(p4);
//com.Parameters.Add(p5);
//com.Parameters.Add(p6);
//com.Parameters.Add(p7);
//com.Parameters.Add(p8);
//conn.Open();
//com.ExecuteNonQuery();
}
答案 0 :(得分:0)
要刷新RadGrid
并查看新添加的行,请使用Rebind()
:
RadGrid1.Rebind();