ADO.NET :: How To Replace The Field Names In SQL Query String With Variables

Aug 17, 2010

How can I replace the field names in sql query string with variables like:-

string field1="ID";
string field2="Name";
sql = "Insert into [Sheet1$] ('" + field1 + "','" + field2 + "') values('1','abc')";

I am inserting data into an Excel document which has got nearly 100 columns.

I am inserting data using OLEDB as follows:-

System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection(connectionstring);
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Insert into [Sheet1$] (ID,Name) values('1','abc')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

I want to specify the field names at runtime as mentioned above - sql = "Insert into [Sheet1$] ('" + field1 + "','" + field2 + "') values('1','abc')"; When I try this I am getting error.

View 12 Replies


Similar Messages:

SQL Server :: Using String Variables As Column Names In A Select Statement?

Dec 20, 2010

I'm trying to use a function in my code behind to let me select a different column each time by calling the function with a different parameter.

However, I keep being told 'invalid column name', even though in the error page the column name appears perfectly valid.

Private Function getData(ByVal s As String) As String

View 4 Replies

C# - Query Filter Design For String Field?

Jun 14, 2010

A field in my table can have arbitrary strings. On the UI, there is a drop down having options like

All, Value1, Value2

And the results were filtered by the selected option value. So far this is easy and adding new filters to the UI is not a problem. Needs no changes in my stored procedure. Now I want to have an "Others" option here as well, which will return rows not having the column value as Value1 or Value2.

Apparently this will require a "not in" operator in my query, and will make maintenance difficult, as the list of values is likely to change

View 2 Replies

Web Forms :: Regex.Replace - How To Replace All In A String

Jun 18, 2010

I need to replace <span> entries in a string to legacy html code because it's going to be used in a report for Crystal Reports. <b> works with Crystal, but the<span>'s do not.

Here's the string which I'm trying to replace: <span style="font-weight: bold">%THIS CAN BE ANY TEXT%</span>. I want to replace it to

<b>%THIS CAN BE ANY TEXT%</b>.

[Code]....

View 5 Replies

DataSource Controls :: Update Table Field With LINQ To SQL And Query String

Jul 11, 2010

I am trying to update my table ARTICLES and it has a field COUNTER, Everytime a user enter this ARTICLES, the page_load event runs a LINQ to SQL query taking the QueryString as parameter, increasing the COUNTER field in 1.

[Code]....

View 2 Replies

Web Forms :: How To Get The Field Names From A List

Nov 15, 2010

I have a list and I want to return all of the field names.

Something like

[Code]....

View 5 Replies

Forms Data Controls :: How To Get Gridview Field Names

Jan 17, 2011

I have a gridview that is dynamically created based off a csv file I'm importing.

Since I don't know what the column names are ahead of time, I can't pre format the gridview and use the "findcontrol" method to get the text value of the labels that are populated b/c I don't know the column names.

I know I can get the row index like this by iterating through the gridview...

[Code]....

but I'm not sure how to get the text value of the first column label? Not sure of the syntax?

View 2 Replies

C# - How To Use Lambda Expression To Select Different Fields From Field Names

Nov 18, 2010

I need to get two fields from a database table (retrieved using linq-to-sql), one field is a datetime (and is a fixed field) and the other is always a decimal, but the field can be different.

The table holds currency data which is processed twice a day and in different currencies so could have fields such as AM_USD, PM_USD, AM_EUR etc. And I need to get data such as a list of the date against PM_USD or the date against AM_EUR.

I would like to be able to call the data using a lambda expression for example (this is a stripped out example):

data = TableData.Select(x=>new {x.DateTimeAdded, x.[**field name as string**]});

I have been trying to write a function to do this, and am failing dismally.

The closest I have managed is:

private Func<TableData, KeyValuePair<DateTime, decimal>> CreateSelect(string FieldName)
{
var parameterExp = Expression.Parameter(typeof(TableData), "sel");
var dateParameter = Expression.Parameter(typeof(DateTime), "DateTimeAdded");
var fieldParameter = Expression.Parameter(typeof(decimal), FieldName);
ConstructorInfo constructorInfo = typeof(KeyValuePair<DateTime, decimal>).GetConstructor(new[] { typeof(DateTime), typeof(decimal) });
NewExpression constructExpression = Expression.New(constructorInfo, new ParameterExpression[] { dateParameter, fieldParameter});
var lambda = Expression.Lambda<Func<TableData, KeyValuePair<DateTime, decimal>>>( constructExpression, parameterExp);
return lambda.Compile();
}

Which fails with "System.InvalidOperationException: Lambda Parameter not in scope".

I'm sure I missing something obvious, or going about it wrong way.

View 2 Replies

How To Determine Corresponding Field Names Of Items In Request.Files

Sep 15, 2010

Given a set of uploaded files in Request.Files, how do figure out which form field yielded which file?

I have a generic form emailer that various forms post to. This file generates an email of the name/value pairs contained in the form post. I'm trying to add support for uploaded files such that the table of name/value pairs will show the name of the file upload element and the name that the file was saved as.

However, I can't figure out how to link that information together. HttpPostedFile doesn't contain any information about the HTTP request (like which field name was used), and Request.Form doesn't contain any entries for uploaded files.

So while I can easily upload the files, I don't have an easy way to generate an email saying "this uploaded file was for this field, and this uploaded file was for that field".

View 1 Replies

How To Replace " With " In A Mysql Query

Apr 2, 2010

I am trying to run this SQL from ASP.NET 2005 but am getting an invalid SQL error because it has a weird character " in it. In my code I am trying to replace " with " but it is not doing it as the special character in the replace command is changing to ".Query:

insert into userquery(description)
values ('In fact, Topeka Google Mayor Bill Bunten expressed it best: "Don't be fooled. Even Google recognizes that all roads lead to Kansas, not just yellow brick ones."')

If I copy and execute this in mysql it is working good.

View 4 Replies

DataSource Controls :: Storing Required Field Names In Web.config?

Feb 2, 2010

I am writing an app that has a bit of process flow to it. On the save of a record, I need to check some fields to make sure they are not null. If they are not null, I will update a 'SectionComplete' field to true.

The thing is, I would like to store my fields in the web.config. So, I would like to have a string in the appsettings section that has a list of field names that are required. Then in my method, I would call these fields out and check each one for the specific record id, and then update the database.

I would like to do this in the web.config so we could add/remove columns without having to redeploy.

For example: my required fields are name, id, and phone. For section one to be labeled complete, all of these fields would need to be not null.

web.config would have this entry:

<add key="Stage1Fields" value="name, id, phone"/>

View 4 Replies

SQL Server :: (sql Query) Replace Non English Chars With %?

Jan 25, 2011

I want to replace some chars like ş,ğ,ı,ç... to % and search with LIKE for easy search and data convenience.But if the string is ayşe and I search all the other things like aysel ay.........e comes in the resut set.Is there any way to change these characters with one char length % ???

View 1 Replies

SQL Server :: How To Find And Replace Substring In Select Query

Sep 22, 2010

how to find and replace substring in select query

DATA RELEASE FORM $LSB$DRF$RSB$

View 2 Replies

How To Pick Specific Field Besides Primarykey Field Through HQL Query

Oct 24, 2010

how to pick a specific field (besides Primarykey field ) through HQL query ???

View 5 Replies

DataSource Controls :: Find And Replace Within Field, Sql Server 2000?

Jun 30, 2010

I have a table called 'products'. In each table there is a column named 'description' of type ntext (sql server 2000). The description conains a ton of text and within that text there is an ip address many times. I need to replace every instance of the ip address with the actual domain name, 123.45.678.990 with http://domain.com.

View 5 Replies

Search And Replace String Value?

Jun 9, 2010

I have a string variable contain text that I would like to search for "Keyword" and replace it with new value.

For example:

Dim strValue = " col1 = "John" and col4 = "smith" or col7 <> "closed" or col9 > 1 and col5 = "passed" "

After the replace the strValue should be :

strValue = " col1 = "John" and ( col4 = "smith" or col7 <> "closed" or col9 > 1 ) and col5 = "passed" "

View 13 Replies

How To Replace Characters In String

Dec 1, 2010

i have a string and want to remove text up to the last forward slash, e.g

/images/UserThumbnails/22.jpg
Should now be
22.jpg

How do i go about doing this?

View 2 Replies

C# - Replace String With A Control?

Feb 15, 2010

I have a string with special characters insert in different places. For example:

string myString = "This is a textbox: ##";

I would like to replace the ## with a control (namely, a textbox).

The Replace method only allows the string to be replaced with another string or character (understandably). But what would be the best way to dynamically replace the ## with a control in its position?

I was thinking maybe I could replace it with HTML markup which would be executed, but not quite sure how that would be achieved.

EDIT: To clarify some details. The strings are being retrieved from a database, so I can't use the PlaceHolder control. The user selects a string from a drop-down list. The value of the item is the string with special characters. When the postback occurs from the item selection, I would like to display the string on the site, but replace the special characters with a fully working control (in this case, a textbox)

View 5 Replies

C# Replace String Not Working?

Oct 4, 2010

// Build email link
confirmLink = Master.siteDomain + "/newsLetter.aspx?action=confirm&e=" + emailAddress + "&code=" + verCode;
using (SqlCommand cmd = new SqlCommand("SELECT newRegEmailBody, newRegEmailSubj FROM tblSiteSettings WHERE (isActive = 1)", Master.cn))
{
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
emailBody = rdr[0].ToString();
emailSubj = rdr[1].ToString();
}
rdr.Close();
}
emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, emailBody);

It all seems to work fine, except the body is still showing up with [CONFIRMATION_LINK] in the text,

View 4 Replies

C# - Replace Char In A String?

Sep 13, 2010

how to change

XXX@YYY.ZZZ into XXX_YYY_ZZZ

One way i know is to use the string.replace(char, char) method,

but i want to replace "@" & "." The above method replaces just one char.

one more case is what if i have XX.X@YYY.ZZZ...

i still want the output to look like XX.X_YYY_ZZZ

View 8 Replies

Web Forms :: Set Required Field Validator For Radio Button Based On Group Names?

Aug 1, 2012

how to set required field validator for radio button based on the  group names

View 1 Replies

MVC :: Create Url Safe Names For Any String In A Database?

Dec 15, 2010

Given a string in my database like "Computer Gadget", I am trying to achieve a url format like "http://xyz.com/products/details/computer-gadget" when a user clicks a link to the item "computer gadget" from a list view.In my list view for the products, I have Action links that read like this

[Code]....

When I hover over this link on the rendered page I get a link that looks like "http://xyz.com/products/details/computer+gadget"and the correct product detail is returned after I have url.decoded the string in my Details method. However, the returned page displays a url that looks like "http://xyz.com/products/details/computer%2bgadget"How can I get the returned url to show "computer-gadget" instead?Also when a name has "&" or "/" the server throws a Http 400 error message. I know these are unsafe values, but is there a way to work around this safely to allow the strings keep these characters?

View 3 Replies

Web Forms :: How To Replace String Using Regex

Jun 16, 2010

I am getting taggings like the following in a string

<color> vocation </color>
<color> elation </color>
<color> sage </color>

you can notice that each tagging differs only in the wordings

vocation
elation
sage

But i want to write a code some thing like this

Regex.Replace(userAx, "<color> vocation </color>",
"<color> </color>"

so that the resulting output does not contain words at the centre.In the above code instead of giving the word vocation exclusively, i have to mention some pattern, so that it will replace all the words instead of doing it for first sentence only.How to modify my code?

View 6 Replies

Finding Wildcard String Replace?

Oct 21, 2010

I am using a chat script I found for free online.

It allows a profanity filter by using string replace:

ex: ChatText.Replace("hell", "!@#$")

is there a way to do a wildcard replace? It doesn't catch the word "HELL" or "Hell" for example.

View 2 Replies

Replace Special Character From String?

Sep 16, 2010

txtPhoneWork.Text.Replace("-","");
txtPhoneWork.Text.Replace("_", "");
txtMobile.Text.Replace("-", "");
txtMobile.Text.Replace("_", "");
txtPhoneOther.Text.Replace("-", "");
txtPhoneOther.Text.Replace("_", "");
location.ContactWork = txtPhoneWork.Text.Trim();
location.ContactMobile = txtMobile.Text.Trim();
location.ContactOther = txtPhoneOther.Text.Trim();

but it is not replacing and is there any method so that both - and _ can be replaced in single function.

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved