Reputation: 2313
When I click the button the information doesn't load into my table.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmManageUsers.aspx.cs" Inherits="frmManageUsers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/CoolBiz_Productions_logo.JPG" PostBackUrl="~/frmMain.aspx" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]">
</asp:SqlDataSource>
</div>
<div align="center">
<asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
<asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
<asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"
onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>U</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Button ID="btnAddUser" runat="server" Text="Add User"
onclick="btnAddUser_Click1" />
</p>
<p>
<asp:Label ID="lblError" runat="server"></asp:Label>
</p>
</div>
<p>
</p>
<div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
SortExpression="UserID"></asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName"></asp:BoundField>
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword"
SortExpression="UserPassword"></asp:BoundField>
<asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"
SortExpression="SecurityLevel"></asp:BoundField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Here is my code for frmManageUsers
public partial class frmManageUsers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddUser_Click1(object sender, EventArgs e)
{
//string userName, userPassword;
if (txtUserName.Text == "" || txtUserName.Text == null)
{
lblError.Text = ("User Name may not be empty");
lblError.ForeColor = System.Drawing.Color.Red;
return;
}
// else
// userName = (txtUserName.Text);
if (txtPassword.Text == "" || txtPassword.Text == null)
{
lblError.Text = ("Password may not be empty");
lblError.ForeColor = System.Drawing.Color.Red;
return;
}
//else
// userPassword = (txtPassword.Text);
Session["UserName"] = txtUserName.Text;
Session["Password"] = txtPassword.Text;
Session["SecurityLevel"] = drpdwnlstSecurityLevel.SelectedValue;
Server.Transfer("frmManageUsers.aspx");
//Server.Transfer("grdUserLogin");
}
protected void drpdwnlstSecurityLevel_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Upvotes: 1
Views: 520
Reputation: 1364
You're SQL DataSource is tied to your DATAGRID control only. I don't see where in your btnAddUser_Click1 event handler you are making any calls to insert the record into the database. You will need to add this missing logic as the existing controls aren't wired up to do this. You have a code gap. You're going to need to perform something close to the following. This is just a sample as I pulled it out of one of my data access layers and modified it for viewing purposes.
string _connectionString = string.Empty;
SqlCommand sqlCommand = new SqlCommand();
SqlConnection sqlConnection = new SqlConnection();
._connectionString = config["connectionString"];
sqlConnection.ConnectionString = "<your connection string>";
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure;
//add your parameters here
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
//Repeat for each parameter in your record.
int returnVal = 0;
sqlCommand.CommandText = "< insert record stored procedure name"
try
{
sqlConnection.Open();
using (sqlCommand)
{
returnVal = sqlCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//possibly put some logging inforaation here
}
finally
{
sqlConnection.Close();
}
PS. In your if statements such as:
if (txtUserName.Text == "" || txtUserName.Text == null){//....}
Try using
if(String.IsNullOrEmpty(txtUserName.Text)){//...}
It's a nifty little method that reduces code.
Upvotes: 1