Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Class 1

public void TNameFunction()
{
     string filenumber=Select ID  from table.
     string filename= "TName"+filenumber;

      Directory.CreateDirectory(path+filename);  -------> This file name error.(filename:TNameSelect ID from table) but I want to (filename:Tname+2 or 3,4 (Tname+filenumber)
       //to do tnamefunction txt code  
 }


from here class2 goes to tname function.

Class 2 : Create function class

static int Indexvalue=(Select ID from table)

public void TName(){
foreach(string table in tablecontrol)
{
if(table1)
name=table1;
else if(table2)
name=table2;
else
name="T" + Indexvalue; -------------> I want to index value is what it comes.
.
.
etc.

}
}



How to solve this problem.

What I have tried:

How to solve this file name number problem?

This write code result.
Filename: TNameSelect ID From table.

But I want to TName3 or TName4 ..etc.
Posted
Updated 12-Nov-18 1:14am
Comments
Mehdi Gholam 12-Nov-18 3:25am    
This is not sql it is not c#, you are mixing the two.

1 solution

string filenumber=Select ID  from table.
This is never going to work.
Here are two beginners articles in CodeProject for connection to a SQL database with C#
Beginners guide to accessing SQL Server through C#[^]
How to connect SQL Database to your C# program, beginner's tutorial[^]

ID being a number should be stored in an int or a long not a string so that line should be something like
C#
long filenumber = GetIDFromTable();
where you will write the function GetIDFromTable by studying the articles I gave you above.

Once you have the file number creating a filename based on it is trivial (see String.Format Method (System) | Microsoft Docs[^])
C#
string filename = string.Format("T{0}", filenumber);

Your code
C#
Directory.CreateDirectory(path+filename);
also won't work. CreateDirectory does what it says ... it creates a directory (folder) not a file. So don't pass a file name into it.

You also need to read the documentation on creating a file or folder - How to: Create a File or Folder (C# Programming Guide) | Microsoft Docs[^]Use Path.Combine to get the fully pathed filename e.g.
C#
string fullFileName = System.IO.Path.Combine(path, fileName);

That should keep you going for a while. You might need to come back with a new question about the code
C#
if(table1)
name=table1;
else if(table2)
name=table2;
but at the moment it's not clear what you are trying to do
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900