List Info

Thread: C-Sharp (C#) Group: Understanding stored procedures




C-Sharp (C#) Group: Understanding stored procedures
user name
2006-10-03 06:17:58
Hi!

As part of my school training I'm experimenting with the
following:

When trying to insert new username and password into a sql
database
(SQL 2000) through C# code I did the following:

private void btnInsertWithStoredProcedure_Click(object
sender,
System.EventArgs e)
		{
			//Outer exception, nothing succeeded
			try
			{
				SqlConnection con = new SqlConnection

("server=localhost;database=Users;uid=geir;pwd=geir&quo
t;);
				con.Open();

                //Inner exception. ExecuteNonQuery failed
				try
				{
                                         //Addressing stored
procedure
					SqlCommand cmdIns = new

SqlCommand("InsUnameAndPword",con);
					cmdIns.CommandType = CommandType.StoredProcedure;

					cmdIns.Parameters.Add("Username",SqlDbType.VarChar,8,txtUsername.Text.T
rim());
					cmdIns.Parameters.Add("Password",SqlDbType.VarChar,8,txtPassword.Text.T
rim());

					cmdIns.ExecuteNonQuery();
				}
				catch(SqlException ex)
				{
					lblInfo.Text = "ExecuteNonQuery failed because:
n" +
						"n" +
						ex.Message;
				}
				finally
				{
                                        //Refresh datagrid
and blank
out textboxes on form
					dgUsers.Refresh();
					txtUsername.Text = "";
					txtPassword.Text = "";
					con.Close();
					lblInfo.Text = "Updated database with new user and
password!";

				}//End Inner exception
				con.Close();
			}
			catch(Exception ex)
			{
				lblInfo.Text = "Errormessage: " + ex.Message;
			}//End Outer Exception

		}

Anybody that could help me out explaining what I miss here?

This is just a simple training case in order to understand
the 'rhytm'
of stored procedures.

I use a sql 2000 database with three fields:

         UserID int(4)
         Username varchar(8)
         Password varchar(8)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Understanding stored procedures
user name
2006-10-03 12:24:28
What is the error message you are getting?  I'm seeing two calls to "con.Close()" which if I'm not mistaken throws an exception.  You should only have to call it once(within the finally block is a good place).

On 10/3/06, Krij < gsb58start.no">gsb58start.no&gt; wrote:

Hi!

As part of my school training I'm experimenting with the following:

When trying to insert new username and password into a sql database
(SQL 2000) through C# code I did the following:

private void btnInsertWithStoredProcedure_Click(object sender,
System.EventArgs e)
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; //Outer exception, nothing succeeded
 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; try
   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp; {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;  SqlConnection con = new SqlConnection

(";server=localhost;database=Users;uid=geir;pwd=geir";);
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  con.Open();

 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  //Inner exception. ExecuteNonQuery failed
&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; try
   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;//Addressing stored procedure
 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  SqlCommand cmdIns = new

SqlCommand(";InsUnameAndPword";,con);
&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp; cmdIns.CommandType = CommandType.StoredProcedure;

&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   cmdIns.Parameters.Add("Username&quot;,SqlDbType.VarChar,8,txtUsername.Text.Trim());
&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; cmdIns.Parameters.Add("Password&quot;,SqlDbType.VarChar,8,txtPassword.Text.Trim());

&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   cmdIns.ExecuteNonQuery();
   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  }
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;  catch(SqlException ex)
   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  lblInfo.Text = "ExecuteNonQuery failed because: n" +
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; "n&quot; +
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; ex.Message;
 &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   }
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  finally
&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  //Refresh datagrid and blank
out textboxes on form
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; dgUsers.Refresh ();
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;  txtUsername.Text = "&quot;;
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; txtPassword.Text = "&quot;;
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; con.Close();
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  lblInfo.Text = "Updated database with new user and password!&quot;;

&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; }//End Inner exception
  ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; con.Close();
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; }
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; catch(Exception ex)
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;  lblInfo.Text = "Errormessage: " + ex.Message;
 &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; }//End Outer Exception

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  }

Anybody that could help me out explaining what I miss here?

This is just a simple training case in order to understand the 'rhytm'
of stored procedures.

I use a sql 2000 database with three fields:

&nbsp;   ; &nbsp; &nbsp;UserID int(4)
&nbsp; &nbsp; &nbsp;   ;Username varchar(8)
 &nbsp; &nbsp; &nbsp; &nbsp;Password varchar(8)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )