C# Sql Command

Virus Eslam 1 Reputation point
2021-08-27T00:24:40.657+00:00

hi guys i make a sql command and sqldatareader in query using this method
Int64 NewTABID = Sql.Instance.GetInt64($"Query String", cnString);
and read data here for textbox
textBox_TABID.Text = NewTABID.ToString();
it work fine but when i make same method in other button i got this error
Incorrect syntax near ','. The label 'Text' has already been declared. Label names must be unique within a query batch or stored procedure.
im sure query 100% work in sql server

Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-08-27T01:20:50.25+00:00

    What does the SqlCommand look like? The one that is causing the problem that is. You do not show that so we cannot be specific about the solution but there is probably a problem there. Since the error says Text there is probably a (syntax) problem with the way you are creating the text for the command.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-08-27T00:58:09.997+00:00

    Usually data operations are in their own class e.g.

    A container

    public class Person  
    {  
        public long Id { get; set; }  
        public string FirstName { get; set; }  
        public string LastName { get; set; }  
        public long AccountNumber { get; set; }  
    }  
    

    Operation class

    public class DataOperations  
    {  
         
    	public static Person GetPerson(int identifier)  
    	{  
    		Person person = new Person();  
      
    		var selectStatement = "SELECT FirstName, LastName, AccountNumber FROM People WHERE Id = @Id";  
    		using var cn = new SqlConnection() { ConnectionString = "TODO" };  
    		var cmd = new SqlCommand() {Connection = cn, CommandText = selectStatement};  
      
    		cmd.Parameters.Add("@Id", SqlDbType.Int).Value = identifier;  
      
    		cn.Open();  
    		  
    		var reader = cmd.ExecuteReader();  
    		  
    		if (reader.HasRows)  
    		{  
    			reader.Read();  
    			person.Id = identifier;  
    			person.FirstName = reader.GetString(0);  
    			person.LastName = reader.GetString(1);  
    			person.AccountNumber = reader.GetInt64(2);  
    		}  
      
    		return person;  
      
    	}  
    }  
    

    Then calling here you can assign property values to controls .Text property for instance.

    var person = DataProviderOperations.GetPerson(1);  
    var accountAsString = person.AccountNumber.ToString();  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.