Hi all,
Did a bit of reading on ADO.NET and wrote a simple kwik app in C# / ASP.Net with Sql Server. Is it ok if I post the code here and you guys take a look and let me know if Im on the right path so far?
.NET - Think Im getting it right. Your thoughts?
-
- Registered User
- Posts: 1387
- Joined: 26 Jan 2006, 02:00
- Location: JHB
- Contact:
.NET - Think Im getting it right. Your thoughts?
Hire A Programmer -|- Just Source Code BlogDeja Moo: The feeling that you've heard this bull before.
-
- Registered User
- Posts: 1387
- Joined: 26 Jan 2006, 02:00
- Location: JHB
- Contact:
Hahaha! Not much of a challenge here, I promise you that! Ok, will post my code...some background info first on the code:
From a database with 1 table called "Users". Has 3 fields:
User_ID
Username
User_Password
All it does is on default1.aspx, it shows all usernames and passwords in the table, with a link to edit a selected one.
On clicking the link, it passes the user id through the querystring to default2.aspx. The username and password a extrracted from the table on the user id and put into text boxes. changes made are updated. Thats it! Also, wont add the html details as I think the code is self explanatory..
Please note that I was learning and messing about trying thins, so I have not used naming conventions for buttons and textboxes etc. Im trying to get the concept 100% right...then will iron out the finer details. Here goes...
From a database with 1 table called "Users". Has 3 fields:
User_ID
Username
User_Password
All it does is on default1.aspx, it shows all usernames and passwords in the table, with a link to edit a selected one.
On clicking the link, it passes the user id through the querystring to default2.aspx. The username and password a extrracted from the table on the user id and put into text boxes. changes made are updated. Thats it! Also, wont add the html details as I think the code is self explanatory..
Please note that I was learning and messing about trying thins, so I have not used naming conventions for buttons and textboxes etc. Im trying to get the concept 100% right...then will iron out the finer details. Here goes...
Last edited by SilverBack on 10 May 2007, 09:17, edited 1 time in total.
Hire A Programmer -|- Just Source Code BlogDeja Moo: The feeling that you've heard this bull before.
-
- Registered User
- Posts: 1387
- Joined: 26 Jan 2006, 02:00
- Location: JHB
- Contact:
THIS IS THE CLASS FOR THE DB CONNECTION
THIS IS THE DEFAULT1.ASPX CODE
THIS IS THE DEFAULT2.ASPX CODE
Code: Select all
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Conn_Details
{
/// <summary>
/// Summary description for db_tasks
/// </summary>
public class db_tasks
{
public db_tasks()
{
//
// TODO: Add constructor logic here
//
}
public static string GetConnectionInfo
{
//return db connection
get { return System.Configuration.ConfigurationManager.AppSettings["IntranetConnectionString"]; }
}
}
}
THIS IS THE DEFAULT1.ASPX CODE
Code: Select all
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Conn_Details;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string selSQL, vData;
//get username and passwords from table
selSQL = "SELECT User_ID, Username, User_Password FROM Users";
//create sql connections
SqlConnection oConn = new SqlConnection(Conn_Details.db_tasks.GetConnectionInfo);
SqlCommand oCommand = new SqlCommand(selSQL, oConn);
oConn.Open();
//execute reader and get contents from sql query
SqlDataReader rsUsers = oCommand.ExecuteReader(CommandBehavior.CloseConnection);
//loop through for all the records
while (rsUsers.Read())
{
//write to screen username and password with option to edit
vData = rsUsers["Username"].ToString() + " : " + rsUsers["User_Password"].ToString() + "<a href='Default2.aspx?User_ID=" + rsUsers["User_ID"].ToString() + "'>edit</a><br/>";
Response.Write(vData);
}
//close connections
oConn.Close();
rsUsers.Close();
}
}
THIS IS THE DEFAULT2.ASPX CODE
Code: Select all
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Conn_Details;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string vUsername, vPassword, selSQL, vData;
Int64 vUser_ID;
//get user id from querystring
vUser_ID = Convert.ToInt64(Request.QueryString["User_ID"]);
//create the sql to get correct username and password on the user id
selSQL = "SELECT Username, User_Password FROM Users WHERE User_ID=" + vUser_ID;
//create connections
SqlConnection oConn = new SqlConnection(Conn_Details.db_tasks.GetConnectionInfo);
SqlCommand oCommand = new SqlCommand(selSQL, oConn);
oConn.Open();
//get data from sql query
SqlDataReader rsUsers = oCommand.ExecuteReader(CommandBehavior.CloseConnection);
//open record
while (rsUsers.Read())
{
//set the text boxes to the correct values
txt_Username.Text = rsUsers["Username"].ToString();
txt_Password.Text = rsUsers["User_Password"].ToString();
}
//set the hidden field to hold the user id
User_ID.Value = vUser_ID.ToString();
//close connections
oConn.Close();
rsUsers.Close();
}
//for updates to username and password
protected void Button1_Click(object sender, EventArgs e)
{
string vUsername, vPassword, vUser_ID, upSQL;
//get values from form
vUser_ID = Request.Form["User_ID"];
vUsername = Request.Form["txt_Username"];
vPassword = Request.Form["txt_Password"];
//create update sql
upSQL = "UPDATE Users SET Username='" + vUsername + "', User_Password='" + vPassword + "' WHERE User_ID=" + vUser_ID;
//create connections to db
SqlConnection oConn = new SqlConnection(Conn_Details.db_tasks.GetConnectionInfo);
SqlCommand oCommand = new SqlCommand(upSQL, oConn);
oConn.Open();
//execute the sql query
oCommand.ExecuteNonQuery();
//redirect user to same screen showing updated values
Response.Redirect("Default2.aspx?User_ID=" + vUser_ID);
}
}
Last edited by SilverBack on 10 May 2007, 09:18, edited 1 time in total.
Hire A Programmer -|- Just Source Code BlogDeja Moo: The feeling that you've heard this bull before.
-
- Registered User
- Posts: 1387
- Joined: 26 Jan 2006, 02:00
- Location: JHB
- Contact:
Thanks Kronos...looks much better!!
Hire A Programmer -|- Just Source Code BlogDeja Moo: The feeling that you've heard this bull before.