I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:
"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."
//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalHost;database=Users;uid=geir;pwd=geir");
//Open database connection
con.Open();
//Create variables to hold values from textboxes
string strUn = txtUsername.Text;
string strPw = txtPassword.Text;
//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)
VALUES(strUn, strPw)";
sqlcmd.Connection = con;
I have a table .. has 3 fields.. first is ID (Autoincrement) the other two are texts like ID | Text1 |Text2 1 |aaa |aaa2 2 |bbb |bbb2 3 |ccc |ccc2
etc.. now there is a change in my requrement I need to add 4 more rows at the beginning like. ID | Text1 | Tex2 1 |xxx |xxx2 2 |yyy |yyy2 ., . 5 |aaa |aaa2 6 |bbb |bbb2
How can I add rows in the beginning by auto adjusting the ID colum. I'm using the enterptice manager to do this>. HELP!
I`m using SQL Server 6.5 and my application does a weekly batch load of data into a database. Since it`s a re-runnable batch process, there is no need for logging, the log files just fill up, cause problems and slow the process down.
Is there a way to do an Insert into a database and have it NOT write to the transaction log?
Alternately, if I just want to dump the transaction log, is there a stored proc for doing that?
I have to post this problem again because I didn't get the correct way.
I have SQL Server 2000 on client (Windows XP) and server (Windows 2003). DTC is running on both side. There is a transcation like,
BEGIN TRAN INSERT MyTable SELECT * FROM MyView COMMIT
MyView get data by:
SELECT * FROM OPENDATASOURCE ('SQLOLEDB', 'Data Source=PROD;UserID=DBDev;Password=WORLDWID').PRODD ata.dbo.CService
PROD is the production server.
When I run this query, there is the error message,
Server: Msg 8501, Level 16, State 1, Line 2 MSDTC on server 'PROD' is unavailable. Server: Msg 7391, Level 16, State 1, Line 2 The operation could not be performed because the OLE DB provider 'SQLOLEDB' was unable to begin a distributed transaction. OLE DB error trace [OLE/DB Provider 'SQLOLEDB' ITransactionJoin::JoinTransaction returned 0x8004d01c].
I wanted to workout with while loop as an alternative of cursor. I am working in SQL Server 2005.
I have a table TEST with following values
tid tname
1 John
2 Sam
3 Peter
I wanted to insert these records into same table or another table using following while loop. But I ended of inserting only last record in the loop, not each one above.
--DELETE FROM test1
DECLARE @id int,
@lc int,@rc int
--SET ROWCOUNT 0
SELECT tid,tname INTO #Temp FROM Test
SET @rc=@@ROWCOUNT
--SET ROWCOUNT 1
--SELECT @id=tid FROM #TEMP
SET @lc=1
WHILE @lc<=@rc
BEGIN
SELECT @id=tid FROM #TEMP
INSERT INTO TEST
SELECT @id,'ppp'
FROM (SELECT tid FROM #TEMP WHERE tid=@id) T
--SELECT * FROM #Temp
SET @lc=@lc+1
END
Any help how to insert each record using while loop will be much appreciated.
I am having a Data flow task in For each loop which will gets 100 sourcetable names and 100 target table names...
am having a simpleData flow task which trasferes from OLEDBSource to OLEDBDestination. I am repeating the Dataflow task which transfers from sourcetablename extracted from for loop to a destination table var.
The problem am gettting is for the first table it is able to transfer correcly because I did mapping for those tables at design time...but for the next coming sourcetable-desttable (which r having different no of cols,datatypes) its giving Validation failed...and...needs to refresh metadata....
is there any way to refresh the metadata of Data flow task (I set the property of OLEDBSource validate external meta to false then also same error is coming)
Which calls for me to import: Microsoft.SqlServer.Dts.Runtime However, I don't have this DST Runtime DLL installed on my machine. I have installed "everything" in SQL Server Developer Edition and Visual Studio. Any idea how I can get this?
I just ran a testbed of 4 types of SQL queries:1. inline SQL with a StringBuilder2. managed sql3. SQL text processing (@SQL as varchar(5000); SET @SQL = 'SELECT ' + @var...)4. a regular sproc that has the columns and table name hard coded1,2, and 4 always end up at about the same time given the averages.3 is always at last 1.5 times slower, and usually closed to 2 times.1 and 2 both use StringBuilders, the code is a direct copy, and 3 is a copy as well.My managed SQL is: [Microsoft.SqlServer.Server.SqlProcedure] public static void usp_Items_Select_Managed(SqlString table, SqlString name, SqlString value) { // sql StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat( "SELECT {0}.* FROM {0} WHERE {0}.{1} = {2}", table, col, value ); SqlConnection sqlConnection = new SqlConnection("context connection=true"); SqlCommand sqlCommand = new SqlCommand(stringBuilder.ToString(), sqlConnection); sqlConnection.Open(); SqlContext.Pipe.Send(sqlCommand.ExecuteReader()); sqlConnection.Close(); }Is there anything wrong with my Managed SQL, or is this just the way that it is?Thanks
I'm running a package on SQL Server 7, SP2 created in the same environment. When running a package from DTS Designer, I get an execution status of "Error Occurred" in the Package Execution Status dialog on one of the data pump tasks I'm running along with the number of rows inserted. When I double-click on the task, I get the following message: " The Connection is currently being used by a task. The connection cannot be closed or reused".
Both the exception log for the task and the package, specified for error output, indicate the task ran successfully and the clients confirm that the appropriate number of rows have been inserted in the table. Has anyone encountered this error that can explain the cause/cure?
When I'm using installshield to install sql runtime, I get the message "The Workstation service has not been started" during Configure SQL Server Agent. Does someone know what it means and how to solve it?
I've an DTS package, which has an Execute SQL Task. I'm doing the operations based on a parameter. I need to pass that in runtime. Is there any way to do that in Execute SQL Task? BOL says that the runtime parameter can be set by giving a question mark(?). But when I tried this, it says, value not passed.
I am Using Sql Server 2005 enterprise edition to develop an accounting system. now i want to distribute this software to other cleints. I have created a setup which includes all the components required for the run time of the software but i dont know how to include the sql server database in it. as i know that i have created access based software i used to include the database file in the setup and it would work but in the case of sql server i dont know how to do it . is there any runtime of sql server which needs to be downloaded or any other way out.
set @t = 'Test' set @chg22 = 'USE <%1> GO' set @chg22 = REPLACE(@chg22,'<%1>',@t)
exec(@chg22) SELECT * FROM Table_Name
but i am not getting it..in above example i am just took one line select * from table_name but actually in .sql file more that 300 lines are there so i m not able to concatenate it..
We run a regular query through Excel that is linked to a Microsoft SQL Server data source.
We have run this query routinely for the past few years. Suddenly this query has started returning an error message - “[Microsoft][ODBC SQL Server Driver] [SQL Server] Difference of two datetime columns caused overflow at runtime�. It seems to be a common error (as per Google search) however I don’t want to go messing around with something I don’t fully understand. Can anyone point me in the direction of what to look out for?
Currently im using SQL queries in combination with ASP.NET..
I have currently only one table('Requests'), which has all the user info of name,id,studyrequested etc.. A person can request for any number of studies.
Now the prob is that from now on whenever a user requests for a new study not only should his details be stored in the Request table but also in a new Users table. In the Users table that particular users details must only be loaded once.. i.e. if the same user enrolls for another study his details should only be updated in the requests table and not in the Users table, since his details have already been loaded once in the Users Table..
While this works fine for new users, but what bout the old users already stored in the Requests table before the Users table existed.
Is there any way i could run a sql script only once at runtime, so that all the old legacy users are loaded once in to the Users Table.
My boss claims that there is a free SQL server runtime. Is thistrue? I was only aware of MSDE. But I believe the connections arecapped at 5 in MSDE. If there is a free SQL server runtime, are thereany limitations to using it (performance, connection count)? Thanks.
I want to combine a bunch of reports (which are stand alone reports on there own) as subreports in a parent or master report. We have done this in Crystal Reprots in order to get a group of reports out in one file.
The issue with Reporting Services is the header and footers of the subreports are not rendered, so I have to repeat the logo information for each subreport in the body of the parent report. I can do this in a table or list or rectangle, but I have to do this for each subreport. Is there any way that I can specify the name of the subreport at runtime? That way I can put everything into a List and drive it from the database. I noticed that the dropdown to select the report name does not allow you to create an expression for this field.
Does anyone have an idea on how to handle runtime parameters for a package? Ideally I'd like to have the package prompt the user for a date range and then store the 2 dates in package variables for use later in the package.
I am trying to create a report with unknown number of report items. (as I dont know how many columns the stored proc is going to return).Is there a way to create these type of reports using SSRS other than creating the rdl programatically using vb.net or c#?
I'm trying to create a custom data flow transformation, I need to set the value of a user variable which will be used by a downstream component(for example: a data flow destination) at runtime, is this possible? If possible, in which method should I do that? Thanks.
Our company is currently looking at a package which uses sql server 2000 as backend and power builder as front end. The vendor said that we need to have SQL client runtime install in the user pc. Can anyone tells me what is SQL client rutime and the purpose of it?
Hi! My question is exactly the subject. My Web Form has only a GridView and a DetailsView, there is no SqlDataSource at project time, i create the SqlDataSource at runtime using code like this in the Page_Load event: (I NEED IT TO BE CREATED DYNAMICALLY)1 Dim SQLDS As SqlDataSource = New SqlDataSource() 2 3 SQLDS.ID = "CustomerDataSource" 4 SQLDS.ConnectionString = ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString 5 SQLDS.SelectCommand = "select customerid,companyname,contactname,country from customers" 6 SQLDS.InsertCommand = "insert into customers(customerid,companyname,contactname,country) values(@customerid,@companyname,@contactname,@country)" 7 SQLDS.UpdateCommand = "update customers set companyname=@companyname,contactname=@contactname,country=@country where customerid=@customerid" 8 SQLDS.DeleteCommand = "delete from customers where customerid=@customerid" 9 10 SQLDS.UpdateParameters.Add(New Parameter("companyname")) 11 SQLDS.UpdateParameters.Add(New Parameter("contactname")) 12 SQLDS.UpdateParameters.Add(New Parameter("country")) 13 SQLDS.UpdateParameters.Add(New Parameter("customerid")) 14 15 Page.Controls.Add(SQLDS) 16 17 If Not Page.IsPostBack Then 18 GridView1.DataKeyNames = New String() {"customerid"} 19 GridView1.DataSourceID = SQLDS.ID 20 21 ' ... and so on The DetailsView1 uses the same SqlDataSource to show data, but i could not find a way to synchronize the DetailsView1 with the GridView1 when a record is selected in the GridView1.How can I synchronize the DetailsView?I played with the ControlParameter but i can't find either how to add a ControlParameter in code, is there a way? Every place talking about ControlParameter shows something like this: 1 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 2 ConnectionString="<%$ ConnectionStrings:Pubs %>" 3 SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = @state"> 4 <SelectParameters> 5 <asp:ControlParameter Name="state" ControlID="DropDownList1" PropertyName="SelectedValue" /> 6 </SelectParameters> 7 </asp:SqlDataSource> 8 Ok. OK. But my SqlDataSource is created dynamically. Any ideas on how to solve this problem?Thanks!
Hi, I am trying to create a DTS package dynamically. I have taken all these steps i.e., 1. SN.EXE -K c:DTS.KEY2.tlbimp.exe "C:program filesmicrosoft SQL Sever80ToolsBindtspkg.dll" /out:c:Microsoft.SQLServer.DTSPkg80.dll /Keyfile:c:DTS.KEY3.gacutil.exe -i C:Microsoft.SQLServer.DTSPkg80.dll. Now when i compile the code, i didnt get any compilation error. But when execute the code it gives me a runtime error which goes as given below : An unhandled exception of type 'System.InvalidCastException' occurred in DTSFactory.exe Additional information: QueryInterface for interface Microsoft.SQLServer.DTSPkg80.CustomTask failed. I am getting this error near the line : oCustTask = (DTS.DataPumpTask2)oTask.CustomTask; The following is the code. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using DTS = Microsoft.SQLServer.DTSPkg80; namespace DTSFactory { /// <summary> /// Summary description for Form1. ///This is assuming that all steps have been taken in the following document: ///http://SQLDEV.NET/DTS/DotNetCookBook.htm ///SN.EXE -K c:DTS.KEY ///tlbimp.exe "C:program filesmicrosoft SQL Sever80ToolsBindtspkg.dll" /out:c:Microsoft.SQLServer.DTSPkg80.dll ///Keyfile:c:DTS.KEY ///gacutil.exe -i C:Microsoft.SQLServer.DTSPkg80.dll ///These steps are needed for interop with dtspkg.dll /// </summary> public class Form3 : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Button button1; public DTS.Package2Class pkg = new DTS.Package2Class(); public Form3() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } private void Form3_Load(object sender, System.EventArgs e) {
In a web site the user has to choose one out of several sql tables to deal with. He will then be allowed to view the selected table data through a GridView, to insert a new row into and to update a row in the selected table by means of an array of TextBoxes created dynamically on the base of the selected table. I think it is possible to solve the problem fully by properly configuring at run time an SqlDataSource. I have solved the issue of data display by declaring in the code behind within the OnPageLoad sub the following: SqlDataSource1.SelectCommand = "Select * FROM " & selectedTable While for the GridView1 I have added the selected table columns to the columns collection as follows: for i=0 to ColumnCount-1 Dim cac As BoundField = New BoundField cac.HeaderText = HeaderNamesArray(i) cac.DataField = ProductNamesArray(i)Me.GridView1.Columns.Add(cac) next I have difficulty on how to do similar declarations for the insertcommand and update command.
Hi I am binding SQLdatasource with the Gridview at runtime that works fine, but i have mentioned the update command as well, but grid is not updating, there is no error, but after clicking update, no thing happens and grid returns to last state. Part of code is : void btnshowdata_click(){ SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; SqlDataSource1.SelectCommand = "dbo.getAllOutstanding"; SqlDataSource1.UpdateCommand = "UPDATE SPS_Oustandings SET Status = @Status, Comments = @Comments WHERE (Inv_No = @Inv_no)"; grid.DataBind(); } datasource is already attached to grid, but its select query is chaning at button click. At design time binding update works but when i click the button binding(select quesry) is changed, after that update is not working.