I want to automatically add parameters based on the input of control numbers.
the following code will give me sp_INSERT #COL5, sp_INSERT #COL4, so on...
control = 5;
while(1<=control)
column = '#COL'
string setValues = "sp_INSERT'" + column + control + "';"
control = control - 1;
What I want to achieve is sp_INSERT #COL5, #COL4, #COL3, so on...
Just... loop?
int control = 5;
string colPrefix = "#COL";
var sql = new StringBuilder("sp_INSERT ").Append(colPrefix).Append(control);
// note first item has different format due to comma
for(int i = control - 1; i > 0; i--)
{
sql.Append(", ").Append(colPrefix).Append(i);
}
sql.Append(';');
string s = sql.ToString();
Simple loop, not a complete solution, but it might help...
string myString = "INSERT ";
for (int i = 5; i > 0; i--)
myString = string.Format("{0} #COL{1}, ", myString, i);
Related
I can't find the error in the code but it showing me.
System.Data.SqlClient.SqlException: 'Incorrect syntax near ')
My code is here:
private void ShowChart()
{
string UserID = "";
for (int Counter = 0; Counter < UID.Count - 1; Counter++)
{
UserID += UID[Counter].ToString() + ",";
}
UserID = UserID.Substring(0, UserID.Length);
string[] ListFamily = { };
int[] ListTime = { };
var Query = Database.Database.SqlQuery<Vw_ShowChartInfo>("Select * From Vw_ShowChartInfo Where UserID In (" + UserID + ")").ToList();
for (int I = 0; I < Query.Count; I++)
{
Family.Add(Query[I].FullName.ToString());
Time.Add(Convert.ToInt32(Query[I].TotalTime));
ListFamily = Family.ToArray();
ListTime = Time.ToArray();
}
this.Chart.Series.Clear();
this.Chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Pastel;
this.Chart.Titles.Add("نمودار کارکرد پرسنل");
for (int II = 0; II < ListFamily.Length; II++)
{
Series series = this.Chart.Series.Add(ListFamily[II] + "-" + Query[II].TotalTime);
series.Points.Add(ListTime[II]);
}
}
I think your problem might be this line:
UserID = UserID.Substring(0, UserID.Length);
The string you are generating contains a comma at the end. It looks like you are attempting to trim the comma with that line but you are simply selecting the same string again. Use this instead:
UserID = UserID.Substring(0, UserID.Length - 1);
Also, the assumption is your user ids are numeric. Otherwise, you would need to surround them with single quotes.
That being said, it's a terrible idea to concatenate strings this way as it leaves you open to SQL injection attacks. Consider a different approach.
I am working on an application that was implemented using a SQLite database. I am currently in the process of adding the ability to use MSSQL as well. The complicated part is that it will need to be able to use either engine depending on the needs. There are a handful of different syntax differences that have to be accounted for. The biggest problem child I have come across is LIMIT vs TOP. I have written some logic to convert the SQLite statements into the proper format for MSSQL. My function for converting the LIMIT to TOP seems to be working, but it ended up being pretty ugly. I wanted to post it here and see if anyone had ideas for a cleaner method of completing this. I also wanted to see if anyone noticed any glaring issues that I have missed. The biggest problem I ran into is the possibility of nested select statements with possible LIMIT statements on them as well. I ended up pulling the statement apart into its individual parts, changing them from LIMIT to TOP, and then rebuilding the statement. There might even be an overall better way to do this that I am missing. Thanks ahead of time if you spend the time to take a look.
private static string ConvertLimitToTop(string commandText)
{
string processCommand = commandText;
int start = -1;
List<string> commandParts = new List<string>();
//Running through the string looking for nested statemets starting with (
for (int i = 0; i < processCommand.Length; i++)
{
//Any time we find a new open ( we want to start there
if (processCommand[i] == '(')
start = i;
//If we find a close ) we will grab the nested statment and replace it
if (processCommand[i] == ')')
{
//Grab the 3 parts of the string
string preString = processCommand.Substring(0, start);
string nestedCommand = processCommand.Substring(start, i - start + 1);
string postString = processCommand.Substring(i + 1);
//Add the nested command to the list
commandParts.Add(nestedCommand);
//Update the commandText replacing the nested command we removed with its index in the list
processCommand = preString + "{" + (commandParts.Count - 1) + "}" + postString;
//Go back the the beginning of the command and look for the next nested command
i = 0;
start = -1;
}
}
//If start isnt -1 that means we found an open ( without a closing )
if (start == -1)
{
//We want to add the final command the the list for processing too
commandParts.Add(processCommand);
//We're going to go through the command parts and replace the LIMIT
for (int i = 0; i < commandParts.Count; i++)
{
string command = commandParts[i];
Console.WriteLine(command);
//We need to find the where the LIMIT is and extact the number
int limitIndex = command.IndexOf("LIMIT");
if (limitIndex != -1)
{
int startIndex = limitIndex + 6;
//Assuming after the limit will be ), a space, or the end of the string
int endIndex = command.IndexOf(')', startIndex);
if (endIndex == -1)
endIndex = command.IndexOf(' ', startIndex);
if (endIndex == -1)
endIndex = command.Length - 1;
Console.WriteLine(startIndex);
Console.WriteLine(endIndex);
//Extract the number
string limitNumber = command.Substring(startIndex, endIndex - startIndex);
//Remove the LIMIT command. There should always be a space before so take that out too.
command = command.Remove(limitIndex - 1, endIndex - limitIndex + 1);
//Insert the top command with the number
command = command.Replace("SELECT", "SELECT TOP " + limitNumber);
//Update the list
commandParts[i] = command;
}
}
start = -1;
//We need to go through the commands in reverse order and reassemble the complete command
for (int i = 0; i < processCommand.Length; i++)
{
//If we find a { its a part of the command that needs to be replaced
if (processCommand[i] == '{')
start = i;
if (processCommand[i] == '}')
{
string startString = processCommand.Substring(0, start);
string midString = processCommand.Substring(start, i - start + 1);
string endString = processCommand.Substring(i + 1);
//Get the index of the command we need from the list
int strIndex = Int32.Parse(midString.Substring(1, midString.Length - 2));
processCommand = processCommand.Replace(midString, commandParts[strIndex]);
//Go back to the start and look for the next
i = 0;
start = -1;
}
}
commandText = processCommand;
}
else
{
LogManager.Write(LogLevel.Error, "Unmatched parentheses were found while processing a SQL command. Command: " + commandText);
}
return commandText;
}
I made a loop:
for (int i = 0; i < sumUSERS; i++ )
{
principal.UserPrincipalName = "bn" + txtbox_cusnumber.Text + "." + txt_user1.Text;
}
In my Form i have Text boxes with the following names :
txt_user1
txt_user2
txt_user3
How can I set the value i in txt_user(i).text?
I hope my question is understandable.
Make an array of the boxes, and use an index, like this:
var txt_user = new [] {txt_user1, txt_user2, txt_user3};
for (int i = 0; i < txt_user.Length ; i++ ) {
principal.UserPrincipalName += "bn" + txtbox_cusnumber.Text + "." + txt_user[i].Text;
}
Note that I replaced = with +=, otherwise the text would be the same as if you used txt_user3 by itself (i.e. only the last assignment would stay).
I'm making an application and i need get the same interval of space from a line in many items (strings). My code is something like that
p = "";
int contaletras = 0;
int maxPalavra = 14; int xpto;
contaletras = item.Length;
xpto = maxPalavra - contaletras;
for (int i = 0; i < xpto; i++)
{
p = p + " ";
}
StringBuilder m = new StringBuilder();
m.Append(item);
m.Insert(item.Length, p);
RTB_Exames.Text = RTB_Exames.Text + m.ToString() + " ";
So, my item.length is all the time 14 characteres. But
i dont know why i have this result when in textvisualizer in c# i have this. I will show some image in this link:
https://drive.google.com/file/d/0B5tH_Qo3-GvhSEJrOXVsTzhIQVk/edit?usp=sharing
https://drive.google.com/file/d/0B5tH_Qo3-GvhaTN5WUM1M2M3QVU/edit?usp=sharing
You can either set the font in the visual editor for the TextBox control or set it via:
RTB_Exames.FontFamily = new FontFamily("Consolas");
Hello I have a DataList that has four lables and they all end with a number 1-4. In my code behind I have a for loop and an array that I want to set the labels of the Datalist with.
for (int x = 0; x< cartreciept.Items.Count; x++)
{
DataListItem item = cartreceipt.Items[x];
string catalogtype = ("select CatalogType From SC Where OrNum=" + OrNum)
if (catalogtype="TC")
{
((Panel)item.FindControl("pnlIprintInfo")).Visible = true;
string scRID = ("Select SCRID From SC Where OrNum =" + OrNum
for(int y = 1; y<5; y++)
{
string lT[y] = (Select LineText From table Where SCartRD =" + scRID + " AND LN=" + y)
((Label)item.FindControl("lbl[y]")).text = lT[y];
}
}
}
So would the ((Label)item.FindControl("lbl[y]")) work? most of this code is just pseudo code until I figure the details out. If you need to know anything else I can provide what needs to be known, I am open to other suggestions as well. Thank you to any one who can offer some help.
You can use this code - based on string.Format("....{0}",arguments)
var control = (Label)item.FindControl(string.Format("lbl{0}",y));
I suggest you to use DataList.ItemDataBound
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx