Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2008, 10:07 PM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default finding error

hi all,

i get error in my application .plz help to recover the error


i add time in the database .before adding in database it
check this time is already exit or not .if exits it don't allow
me to add the same time again

i have two comobox one for hour and second for minute and it contain
int value.i get value from comobox and concatenate both value
and send to database

on running application it give me system error message

private void btnAdd_Click(object sender, System.EventArgs e)
        {
    //cbxHr.Text hour combo box
    //cbxMin.Text min combo box
    SaveTiming(cbxHr.Text,cbxMin.Text);
            }


    private void SaveTiming(string hr, string std_min)
        {
    bool flag = false;
    string connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Testing;Data Source=(local)";
    string test = hr+""+ std_min;

string i****ist = "SELECT * FROM timing WHERE s_time=" + test + "";
string sqlInst = "Insert into timing(s_test)Values(" + test + ")";

using (SqlConnection connection = new SqlConnection(connectionString))
            {
    SqlCommand command = new SqlCommand(i****ist, connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    if (reader.HasRows)
    {
    MessageBox.Show("Timing already has been assigned");
    flag = true;
                }
    reader.Close();
       if (!flag)
                {
        SqlCommand cmd = new SqlCommand(sqlInst, connection);
                    cmd.ExecuteNonQuery();

                }
            }
        }








 
Old April 2nd, 2008, 12:51 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, never concatenate SQL strings like that! Use @param place holders and SqlCommand.Parameters collection. For example:

SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable WHERE Foo = @foo");
cmd.Parameters.AddWithValue("foo", some_value);

Second, I'd suggest you to make stored procedure which handles inserting. Something like:

Code:
CREATE PROCEDURE InsertTiming(@timing nvarchar(255))
BEGIN
  SET NOCOUNT ON;

  -- Check if timing already exists
  DECLARE @tmp nvarchar(255);
  SELECT @tmp = s_time FROM timing WHERE s_time = @timing;
  IF @tmp IS NOT NULL
  BEGIN
    raiserror(N'Timing %s already assigned',
      10,
      1,
      @timing);
  END

  -- Insert new value
  INSERT INTO timing(s_test) VALUES (@timing);
END
Then just call it like this:

Code:
SqlCommand cmd = new SqlCommand("InsertTiming", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("timing", test);

try
{
  cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
  MessageBox.Show(e.Message);
}
 
Old April 2nd, 2008, 12:53 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah. Stupid me. You could just add unique index to s_test field and catch the exception which is thrown when you try to insert duplicate value :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Column Name prabhakaran SQL Server 2000 3 December 4th, 2006 09:36 AM
error in finding ordinal 5077 in mfc42d.dll dylan Visual C++ 0 January 2nd, 2005 02:09 PM
Finding out the computer name lguzman Access VBA 8 July 14th, 2004 02:53 PM
Finding the maximum and using it outside the IF jrwarwick XSLT 2 October 17th, 2003 03:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.