Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, March 7, 2012

how initialize a connection string in .Net

i am using a control that i downloaded from the control gallery It has a line for a connection string.

which i put on in there:

data source=server;initial catalog=database;user id=blahblah;password=557896
and i get an error:

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: The ConnectionString property has not been initialized.]
System.Data.SqlClient.SqlConnection.Open() +433
Microsoft.Vote.VoteButtonClick(Object sender, EventArgs e) +289
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277

How would i initialize the connection string.

any help would be appreciated

aluckeHi,
normally we would have initialized the connection by doing this

dim conn as new sqlconnection("data source=server;initial catalog=database;user id=blahblah;password=557896")

if you still experience error can you print out your codes too?

Friday, February 24, 2012

How I can obtain the data from files Exel, throw ODBC provider?

What will be "Query string"?
ThankHello Sergey,
> What will be "Query string"?
> Thank
>
The query string will be like a typical SQL Select statement, except instead
of a table name you specify a named range from which the data should be drawn.
If you have a grid in an Excel sheet that looks like this:
ID FirstName LastName
1 George Washington
2 John Adams
3 Thomas Jefferson
And you define those cells as named range "Presidents", then:
SELECT ID, FirstName, LastName FROM [Presidents]
Here's some more info on ODBC/Excel. It targets ASP developers, but some
of the facts here may apply to you:
http://support.microsoft.com/kb/q195951/
-Chri

Sunday, February 19, 2012

How have strings within a string variable?

This works fine:

SELECT *
FROM MyTable
WHERE Code IN ('X', 'Y', 'Z')


But I want those values in a variable. If I do the below, it doesn't work because the X, Y, and Z aren't their own string:

DECLARE @.MyString varchar(50)
SET @.MyString = 'X, Y, Z'

SELECT *
FROM MyTable
WHERE Code IN (@.MyString)

Any way I can do what I'm trying to do?

Thanks for any help,

Ron

You couldn't use variables with IN statement. See this quote:

test_expression [ NOT ] IN ( subquery | expression [ ,...n ] )
expression[ ,... n ]

Is a list of expressions to test for a match. All expressions must be of the same type as test_expression.

Try to use dynamic SQL:

DECLARE @.MyString varchar(50)

SET @.MyString = '''X'', ''Y'', ''Z''' --You also need use quotes for each case

DECLARE @.MyQuery varchar(200)

SET @.MyQuery = '

SELECT *

FROM #MyTable

WHERE Code IN ('+@.MyString+')'

EXECUTE (@.MyQuery)

|||

Another method of breaking your strings into pieces is to use Jens Suessmeyer's SPLIT function. The advantage of this method is that it avoids potential SQL injection problems. An example of the split function can be found here:

http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=419984&SiteID=17

Here are a couple of other examples of how the split function might be applied:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1314989&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1337001&SiteID=1

Still another way to do this would be to store your list as a blocked list of strings like in this post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1418032&SiteID=1

|||

An alternative approach might be to do it like this:

declare @.aList table (n int identity)
insert into @.aList default values
insert into @.aList default values
insert into @.aList default values

declare @.myTable table
( rid int,
target varchar(2)
)
insert into @.myTable
select 1, 'A' union all
select 2, 'B' union all
select 3, 'X' union all
select 4, 'Z'

declare @.myString varchar(20)
set @.myString = 'X Y Z '

select a.*
from @.myTable a
inner join @.aList b
on target = rtrim(substring(@.myString, 2*n-1, 2))

-- rid target
-- --
-- 3 X
-- 4 Z