Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu23-Jan-17 0:45
professionalTarunKumarSusarapu23-Jan-17 0:45 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Pete O'Hanlon23-Jan-17 0:46
mvePete O'Hanlon23-Jan-17 0:46 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Simon_Whale23-Jan-17 0:47
Simon_Whale23-Jan-17 0:47 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu23-Jan-17 0:54
professionalTarunKumarSusarapu23-Jan-17 0:54 
SuggestionRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Richard Deeming23-Jan-17 3:17
mveRichard Deeming23-Jan-17 3:17 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu23-Jan-17 18:10
professionalTarunKumarSusarapu23-Jan-17 18:10 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu23-Jan-17 18:57
professionalTarunKumarSusarapu23-Jan-17 18:57 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Richard Deeming24-Jan-17 2:05
mveRichard Deeming24-Jan-17 2:05 
For a start, don't store connection and command objects in fields. Instead, create them when you need them, and wrap them in a using block to ensure that they're always disposed of properly.

You don't need to call ExecuteReader to get the value of the first column of the first row; use ExecuteScalar[^] instead.

And you need to add a params parameter[^] to your method to pass parameters:
C#
private static void PrepareCommand(SqlCommand command, string commandText, object[] parameters)
{
    if (parameters != null && parameters.Length != 0)
    {
        string[] parameterNames = new string[parameters.Length];
        for (int index = 0; index < parameters.Length; index++)
        {
            string name = "@p" + index;
            parameterNames[index] = name;
            command.Parameters.AddWithValue(name, parameters[index]);
        }
        
        commandText = string.Format(commandText, parameterNames);
    }
    
    command.CommandText = commandText;
}

public string get_data(string commandText, params object[] parameters)
{
    using (var connection = new SqlConnection(ConnectionString))
    using (var command = new SqlCommand(string.Empty, connection))
    {
        PrepareCommand(command, commandText, parameters);
        
        connection.Open();
        object result = command.ExecuteScalar();
        return Convert.ToString(result);
    }
}

You can then pass parameters to the command using the auto-generated names:
C#
cfs.get_data("select top 1 AVERAGE_COST from PRODUCT where PRODUCT_NO = @p0", strpcode)

or by position:
C#
cfs.get_data("select top 1 AVERAGE_COST from PRODUCT where PRODUCT_NO = {0}", strpcode)




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu24-Jan-17 0:24
professionalTarunKumarSusarapu24-Jan-17 0:24 
AnswerRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Gerry Schmitz23-Jan-17 8:39
mveGerry Schmitz23-Jan-17 8:39 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu23-Jan-17 18:13
professionalTarunKumarSusarapu23-Jan-17 18:13 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Gerry Schmitz23-Jan-17 19:07
mveGerry Schmitz23-Jan-17 19:07 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu24-Jan-17 0:52
professionalTarunKumarSusarapu24-Jan-17 0:52 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
Gerry Schmitz24-Jan-17 2:36
mveGerry Schmitz24-Jan-17 2:36 
AnswerRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
palikhelsanjeeb25-Jan-17 21:13
palikhelsanjeeb25-Jan-17 21:13 
GeneralRe: How to save values in table in SQL without crashing when it is used by multiple users Pin
TarunKumarSusarapu29-Jan-17 19:00
professionalTarunKumarSusarapu29-Jan-17 19:00 
QuestionHaving trouble closing and reopening of communication port RS232 Pin
goldsoft21-Jan-17 1:40
goldsoft21-Jan-17 1:40 
AnswerRe: Having trouble closing and reopening of communication port RS232 Pin
Afzaal Ahmad Zeeshan21-Jan-17 2:04
professionalAfzaal Ahmad Zeeshan21-Jan-17 2:04 
GeneralRe: Having trouble closing and reopening of communication port RS232 Pin
goldsoft22-Jan-17 10:19
goldsoft22-Jan-17 10:19 
AnswerRe: Having trouble closing and reopening of communication port RS232 Pin
Bernhard Hiller22-Jan-17 21:20
Bernhard Hiller22-Jan-17 21:20 
AnswerRe: Having trouble closing and reopening of communication port RS232 Pin
Gerry Schmitz23-Jan-17 8:17
mveGerry Schmitz23-Jan-17 8:17 
QuestionC# Project Suggestion ! Pin
TEProjects20-Jan-17 23:49
professionalTEProjects20-Jan-17 23:49 
AnswerRe: C# Project Suggestion ! Pin
Richard MacCutchan21-Jan-17 0:06
mveRichard MacCutchan21-Jan-17 0:06 
GeneralRe: C# Project Suggestion ! Pin
TEProjects23-Jan-17 1:41
professionalTEProjects23-Jan-17 1:41 
AnswerRe: C# Project Suggestion ! Pin
NotPolitcallyCorrect21-Jan-17 1:14
NotPolitcallyCorrect21-Jan-17 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.