我想要做的是创建一个页面,如果从用户那里获得一些输入(名称,姓氏),用户将单击提交按钮,系统将检查与te页面连接的数据库,如果有这些人凭证已在数据库中,然后显示它。 我已经使用VS2013连接了数据库,并且数据库表一直显示在页面中,但我想在单击提交后显示它。
我的一些代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2014ConnectionString"].ConnectionString;
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * FROM [dbo].[users] WHERE FirstName= TextBox1";
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, " users ");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables["users"].NewRow();
drow["FirstName"] = TextBox2.Text;
drow["LastName"] = TextBox2.Text;
ds.Tables["TableName "].Rows.Add(drow);
da.Update(ds, " users ");
}
catch(Exception EX)
{
string A = "";
}
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks2014ConnectionString %>"
SelectCommand="SELECT [FirstName], [LastName], [Gender] FROM [users]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="FirstName"
HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName"
HeaderText="Last Name" SortExpression="LastName" />
</Columns>
<FooterStyle BackColor="#5D7B9D"
Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775"
ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6"
Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>