|
 |
aspdotnet_website_programming thread: problem with databases
Message #1 by "Lampros Eleftherios" <lampros@c...> on Thu, 25 Apr 2002 23:15:04 +0300
|
|
Hello to all of you.
I am creating a registration form with ASP.NET and C#. The "systems" works
fine but i don't want to insert a "data" that already is written to the
database.
I used this script:
----------------------------------------------------------------------------
----
<script language="C#" runat="server">
SqlConnection myConnection;
protected void Page_Load(Object Src, EventArgs E)
{
myConnection = new
SqlConnection("server=localhost;database=dok_db;Trusted_Connection=yes");
}
public void Add_Click(Object sender, EventArgs E)
{
if (Page.IsValid)
{
String insertCmd = "insert into user_name (user_first_name,
user_last_name) values (@FName, @LName)";
SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@FName",
SqlDbType.NVarChar, 50));
myCommand.Parameters["@FName"].Value = user_first_name.Value;
myCommand.Parameters.Add(new SqlParameter("@LName",
SqlDbType.NVarChar, 50));
myCommand.Parameters["@LName"].Value = user_last_name.Value;
}
}
</script>
----------------------------------------------------------------------------
----
Well. The code above sends entries to user_first_name and user_last_name and
stores them to the database dok_db.
I don't want to add entries with the same user last or first name. Does
anybody of you know how to do it? Please help me.
Thank you in advanced.
Message #2 by "Robert Horn" <robertho@m...> on Sat, 27 Apr 2002 08:20:52 +1000
|
|
Dear Lampros,
The way that I would do it is to put a unique index on the First and
Lastname together in the database. That way when someone tries to enter a
name that already is there, the database will generate an error. You should
then put a Try - Catch command before you execute your Insert Command to
catch the error and display a message that the First and Lastname are
already in use.
The other way to do this is to do a Select command before you do the insert
to see if the names are already there.
Hope that helps
Robert
----- Original Message -----
From: "Lampros Eleftherios" <lampros@c...>
To: "Website Programming with ASP.NET"
<aspdotnet_website_programming@p...>
Sent: Friday, April 26, 2002 6:15 AM
Subject: [aspdotnet_website_programming] problem with databases
> Hello to all of you.
> I am creating a registration form with ASP.NET and C#. The "systems" works
> fine but i don't want to insert a "data" that already is written to the
> database.
> I used this script:
>
> --------------------------------------------------------------------------
--
> ----
>
> <script language="C#" runat="server">
>
> SqlConnection myConnection;
>
> protected void Page_Load(Object Src, EventArgs E)
> {
> myConnection = new
> SqlConnection("server=localhost;database=dok_db;Trusted_Connection=yes");
>
>
> }
>
> public void Add_Click(Object sender, EventArgs E)
> {
>
>
> if (Page.IsValid)
> {
>
> String insertCmd = "insert into user_name (user_first_name,
> user_last_name) values (@FName, @LName)";
>
> SqlCommand myCommand = new SqlCommand(insertCmd,
myConnection);
>
>
> myCommand.Parameters.Add(new SqlParameter("@FName",
> SqlDbType.NVarChar, 50));
> myCommand.Parameters["@FName"].Value = user_first_name.Value;
>
> myCommand.Parameters.Add(new SqlParameter("@LName",
> SqlDbType.NVarChar, 50));
> myCommand.Parameters["@LName"].Value = user_last_name.Value;
>
> }
>
> }
>
> </script>
>
> --------------------------------------------------------------------------
--
> ----
>
> Well. The code above sends entries to user_first_name and user_last_name
and
> stores them to the database dok_db.
> I don't want to add entries with the same user last or first name. Does
> anybody of you know how to do it? Please help me.
>
> Thank you in advanced.
>
>
|
|
 |