[英]Inserting a record in an SQL Server database using session in aspnet with C#
I'm trying to insert a record into a SQL Server 2014 database using ASP.NET with C#.我正在尝试使用 ASP.NET 和 C# 将记录插入到 SQL Server 2014 数据库中。
I have implemented a session for my gridview data, and I'm trying to insert that session into the database, but when I click the button "book", only the top URL changes:我已经为我的 gridview 数据实现了一个会话,我正在尝试将该会话插入到数据库中,但是当我单击“book”按钮时,只有顶部的 URL 发生了变化:
Database:数据库:
The record isn't getting inserted into the database nor my Label (Errorm) is changing to gg.记录没有插入到数据库中,我的标签 (Errorm) 也没有更改为 gg。
.aspx
file: .aspx
文件:
<%@ Page Title=""
Language="C#"
MasterPageFile="~/Main.Master"
AutoEventWireup="true"
CodeBehind="hotels.aspx.cs"
Inherits="Hotel_Mangement.hotels" %>
<asp:Content ID="Content1" ContentPlaceHolderID="hotels" runat="server">
<div class="destinations">
<div class="destination-head">
<div class="wrap">
<h3>Hotels</h3>
</div>
<!-- End-destinations -->
<div class="find-place dfind-place">
<div class="wrap">
<div class="p-h">
<span>FIND YOUR</span>
<label>HOTEL</label>
</div>
<!-- strat-date-picker -->
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(function () {
$("#<%= txtstart.ClientID %>").datepicker();
});
</script>
<!-- /End-date-picker -->
<!-- strat-date-picker -->
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(function () {
$("#<%= txtend.ClientID %>").datepicker();
});
</script>
<!---/End-date-piker---->
<div class="p-ww">
<form>
<span> City or Area</span>
<asp:DropDownList ID="dl1" runat="server" class="dest" required="This field cannot be blank">
<asp:ListItem Selected="True">Location</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Goa</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Ahmedabad</asp:ListItem>
<asp:ListItem>Jammu</asp:ListItem>
<asp:ListItem>Jharkhand</asp:ListItem>
<asp:ListItem>Kerala</asp:ListItem>
<asp:ListItem>Bhuj</asp:ListItem>
<asp:ListItem>Bengaluru</asp:ListItem>
<asp:ListItem>Kalyan</asp:ListItem>
</asp:DropDownList><br />
<br /><span> Check-in</span>
<asp:TextBox ID="txtstart" runat="server" class="date" required="This field cannot be blank"></asp:TextBox>
<span> Check-out</span>
<asp:TextBox ID="txtend" runat="server" class="date" required="This field cannot be blank"></asp:TextBox><br /> <br />
<span> Number of rooms</span>
<asp:DropDownList ID="dlrooms" runat="server" required="This field cannot be blank">
<asp:ListItem Selected="True">Select number of rooms</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList><br /><br />
<span> Number of members</span>
<asp:DropDownList ID="dlmumbers" runat="server" required="This field cannot be blank">
<asp:ListItem Selected="True">Select number of members per room</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:DropDownList>
<br /> <br />
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
</form>
</div>
<div class="clear"> </div>
</div>
</div>
<!----//End-find-place---->
</div>
<div class="criuse-main">
<div class="wrap">
<div class="criuse-head1">
<h3>CHEAPEST HOTELS</h3>
</div>
</div>
</div>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString='<%$ ConnectionStrings:RegisterConnectionString15 %>'
SelectCommand="SELECT * FROM [hotels_main]"></asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<div ID="div1" runat="server">
<div class="criuse-main" >
<div class="wrap">
<div class="criuse-grids">
<div class="criuse-grid">
<div class="criuse-grid-head">
<div class="criuse-img">
<div class="criuse-pic">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImagePath") %>' Height="350px" width="1000px"/>
</div>
<div class="criuse-pic-info">
<div class="criuse-pic-info-top">
<div class="criuse-pic-info-top-weather">
<p>33<label>o</label><i>c</i><span> </span></p>
</div>
<div class="criuse-pic-info-top-place-name">
<h2><span><%#Eval("hotel_location") %></span></h2>
</div>
</div>
<div class="criuse-pic-info-price">
<p><span>Starting From</span> <h4><%#Eval("price") %> $</h4></p>
</div>
</div>
</div>
<div class="criuse-info">
<div class="criuse-info-left">
<ul>
<li><a class="c-hotel" href="#"><span> </span><%#Eval("rooms_available") %></a></li>
<li><a class="c-air" href="flight.aspx"><span> </span> Air Ticket</a></li>
<li><a class="c-fast" href="#"><span> </span> Guest per room:<%#Eval("max_guest") %></a></li>
<li><a class="c-car" href="#"><span> </span> Car for All transfers</a></li>
<div class="clear"> </div>
</ul>
</div>
<div class="clear"> </div>
</div>
</div>
<div class="criuse-grid-info">
<h1> <a href="hotels_main.aspx?id=<%#Eval("hotel_id") %>" ><%#Eval("hotel_name") %></a></h1>
<p><%#Eval("s_desc") %> </p>
</div>
</div>
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<center>
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" class="myGridClass" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:TemplateField HeaderText="HotelName">
<ItemTemplate>
<asp:Label ID="lblhotelname" runat="server" Text='<%# Bind("hotel_name") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HotelLocation">
<ItemTemplate>
<asp:Label ID="lblhotellocation" runat="server" Text='<%# Bind("hotel_location") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblprice" runat="server" Text='<%# Bind("price") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
<br />
<form>
<asp:Button ID="book" runat="server" Text="Book now" class="d-next" OnClick="book_Click" />
</form>
<asp:Label ID="lprice" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:Label ID="lcheckin" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:Label ID="lcheckout" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:Label ID="lmembers" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:Label ID="lrooms" runat="server" Text="Label" Visible="False"></asp:Label>
<br />
<asp:Label ID="Errorm" runat="server" Text="Label"></asp:Label>
</div>
</center>
</div>
</asp:Content>
.aspx.cs
code-behind: .aspx.cs
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace Hotel_Mangement
{
public partial class hotels : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=RISHIK\SQLEXPRESS;Initial Catalog=Register;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Visible = false;
/* div1.Visible = true; */
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from hotels_main";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
book.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
string query =
"select hotel_name, hotel_location ,price from hotels_main where hotel_location='" +
dl1.Text + "' ";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Visible=true;
con.Close();
if (ds.Tables[0].Rows.Count == 0)
{
Label1.Visible = true;
Label1.Text = "No data found";
book.Visible = false;
}
else
{
/* div1.Visible = true;*/
Label1.Visible = false;
book.Visible = true;
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("HotelName", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("HotelLocation", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
foreach (GridViewRow row in GridView1.Rows)
{
Label lblhotelname = (Label)row.FindControl("lblhotelname");
Session["hotelname"] = lblhotelname.Text;
Label lblhotellocation = (Label)row.FindControl("lblhotellocation");
Session["hotelocation"] = lblhotellocation.Text;
Label lblprice = (Label)row.FindControl("lblprice");
Session["price"] = lblprice.Text;
dr = dt.NewRow();
dr[0] = lblhotelname.Text;
dr[1] = lblhotellocation.Text;
dr[2] = lblprice.Text;
dt.Rows.Add(dr);
}
Session["check_in"] = txtstart.Text.ToString();
Session["check_out"] = txtend.Text.ToString();
}
}
/*private void SendGridInfo()
{
}
Session["GridData"] = dt; Response.Redirect("WebForm2.aspx");
} */
protected void book_Click(object sender, EventArgs e)
{
con.Open();
string insertQuery = "Insert into hotelbook_details values('" + Session["USER_ID"].ToString() + "','" + Session["hotelname"].ToString() + "','" + Session["hotelocation"].ToString() + "','" + Session["check_in"].ToString() + "','" + Session["check_out"].ToString() + "','" + Session["price"].ToString() + "')";
SqlCommand cmd1 = new SqlCommand(insertQuery, con);
cmd1.ExecuteNonQuery();
con.Close();
Errorm.Text = "gg";
}
}
}
The problem was the <form> ..... </form>
tag in aspx.问题是 aspx 中的
<form> ..... </form>
标签。 As I am using a master page I don't need it.当我使用母版页时,我不需要它。
After removing all the form tags, it worked all fine.删除所有表单标签后,一切正常。
Are you sure it is not saving?你确定它不保存? Have you checked your database?
你检查过你的数据库吗?
Change your load page to:将您的加载页面更改为:
if (IsPostBack == false){
GridView1.Visible = false;
/* div1.Visible = true; */
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from hotels_main";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
book.Visible = false;
}
When you click a button it will reload the form load.
当您单击按钮时,它将重新加载表单加载。 The code above will prevent it from loading when doing a post-back, that is, clicking a button.
上面的代码将阻止它在执行回发时加载,即单击按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.