amillyard
asked on
return a record found or not found in sql database / asp.net / c#
Development Platform: c#, asp.net 2.x, Visual Studio Pro utilising Web Developer, IIS 6, SQL Server 2005
Want to be able to enter a case/account number (via a form text field), press the submit button (to search/find account).
-- return back a message indicating 'record not found' OR 'record found' -- no need to display the actual entry -- just need to confirm if that entry is in the database somewhere.
Below I have a script that worked with finding the first successful response in database -- as we are searching for unique account numbers within database, assuming this is still the better way forward for getting some of the account details.
Is this the right scripting to do this -- and if so, what do I need to add for checking for a record find or not variable?
Thank you in advance for your time and efforts with this enquiry.
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne ctionStrin g = ConfigurationManager.Conne ctionStrin gs["FORTUN EConnectio nString"]. ToString() ;
string MasterAccountSearch_ID = "";
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT [MasterAccount_ID] FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}'", TextBox_SearchMasterAccoun ts.Text)), FORTUNEConnection1);
MasterAccountSearch_ID = (string)scCommand.ExecuteS calar();
FORTUNEConnection1.Close() ;
}
Want to be able to enter a case/account number (via a form text field), press the submit button (to search/find account).
-- return back a message indicating 'record not found' OR 'record found' -- no need to display the actual entry -- just need to confirm if that entry is in the database somewhere.
Below I have a script that worked with finding the first successful response in database -- as we are searching for unique account numbers within database, assuming this is still the better way forward for getting some of the account details.
Is this the right scripting to do this -- and if so, what do I need to add for checking for a record find or not variable?
Thank you in advance for your time and efforts with this enquiry.
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne
string MasterAccountSearch_ID = "";
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format(
MasterAccountSearch_ID = (string)scCommand.ExecuteS
FORTUNEConnection1.Close()
}
Is TextBox_SearchMasterAccoun ts.Text the masteraccount id?
ASKER
yes ... but let me clarify please as follows:
TextBox_SearchMasterAccoun ts.Text is what is entered on the asp form -- i.e. type in the account number (which is the same as the primary key field in the database table)
MasterAccount_ID -- is the database table
both variables are representing the same value item.
TextBox_SearchMasterAccoun
MasterAccount_ID -- is the database table
both variables are representing the same value item.
Instead of SELECT [MasterAccount_ID] FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
try SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
This will definitely return a value. If it is 0 the records exists and if it is 1 the masterstyle id is valid
try SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
This will definitely return a value. If it is 0 the records exists and if it is 1 the masterstyle id is valid
ASKER
for some reason, I am now getting an error when trying to compile
-- Use of unassigned local variable 'MasterAccountSearch' --
protected void Button_ClientCase_Click(ob ject sender, EventArgs e)
{
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne ctionStrin g = ConfigurationManager.Conne ctionStrin gs["FORTUN EConnectio nString"]. ToString() ;
string MasterAccountSearch;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
try
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}'", TextBox_SearchMasterAccoun ts.Text)), FORTUNEConnection1);
MasterAccountSearch = (string)scCommand.ExecuteS calar();
}
catch (Exception ex)
{
SQL_FailureHandler(ex);
}
finally
{
FORTUNEConnection1.Close() ;
}
switch (MasterAccountSearch)
{
case "0" : Label_MasterAccountSearchS tatus.Text = "Record Found";
Session["fortuneMasterAcco unt"] = TextBox_SearchMasterAccoun ts.Text;
Response.Redirect("default .aspx");
break;
default : Label_MasterAccountSearchS tatus.Text = "Record NOT Found";
break;
}
}
}
private void SQL_FailureHandler(Excepti on ex)
{
// TODO SQL Failure Handler needs to be scripted
Label_MasterAccountSearchS tatus.Text = "[ Database Search FAILURE ]";
}
}
-- Use of unassigned local variable 'MasterAccountSearch' --
protected void Button_ClientCase_Click(ob
{
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne
string MasterAccountSearch;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
try
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format(
MasterAccountSearch = (string)scCommand.ExecuteS
}
catch (Exception ex)
{
SQL_FailureHandler(ex);
}
finally
{
FORTUNEConnection1.Close()
}
switch (MasterAccountSearch)
{
case "0" : Label_MasterAccountSearchS
Session["fortuneMasterAcco
Response.Redirect("default
break;
default : Label_MasterAccountSearchS
break;
}
}
}
private void SQL_FailureHandler(Excepti
{
// TODO SQL Failure Handler needs to be scripted
Label_MasterAccountSearchS
}
}
Just say string MasterAccountSearch = ""; instead of string MasterAccountSearch;
ASKER
I see, interesting -- worked first time ! (the "" advise)
Compiling ok now -- when running the script -- not getting any success yet with checking record -- keeps indicating that no record was found.
yet, I know I am entering the correct data-entry figure matching the record itself (i.e. 1, 2, 3 etc...)
Compiling ok now -- when running the script -- not getting any success yet with checking record -- keeps indicating that no record was found.
yet, I know I am entering the correct data-entry figure matching the record itself (i.e. 1, 2, 3 etc...)
Did you do this????
Instead of SELECT [MasterAccount_ID] FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
try SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
Instead of SELECT [MasterAccount_ID] FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
try SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}
ASKER
I think so as follows:
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}'", TextBox_SearchMasterAccoun ts.Text)), FORTUNEConnection1);
SqlCommand scCommand = new SqlCommand((String.Format(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok -- got this scripting to work.
I needed to also update one of the string variables to integers as follows:
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne ctionStrin g = ConfigurationManager.Conne ctionStrin gs["FORTUN EConnectio nString"]. ToString() ;
int MasterAccountSearch = -1;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT count(*) FROM [MasterAccounts] WHERE [MasterAccount_ID] = '{0}'", TextBox_SearchMasterAccoun ts.Text)), FORTUNEConnection1);
MasterAccountSearch = (int)scCommand.ExecuteScal ar();
FORTUNEConnection1.Close() ;
if (MasterAccountSearch == 1)
{
Label_MasterAccountSearchS tatus.Text = "Record Found";
Session["fortuneMasterAcco unt"] = TextBox_SearchMasterAccoun ts.Text;
Response.Redirect("default .aspx");
}
else
Label_MasterAccountSearchS tatus.Text = "Record NOT Found";
}
I needed to also update one of the string variables to integers as follows:
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne
int MasterAccountSearch = -1;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format(
MasterAccountSearch = (int)scCommand.ExecuteScal
FORTUNEConnection1.Close()
if (MasterAccountSearch == 1)
{
Label_MasterAccountSearchS
Session["fortuneMasterAcco
Response.Redirect("default
}
else
Label_MasterAccountSearchS
}
ASKER
thank you very much for your time and efforts :-)