C# - Use Sql Command More Than Once With Different Commands?
Mar 28, 2011
I've been trying to use the same SqlConnection and SqlCommand objects to execute to different commands.
the first one checks for duplicate and the second one inserts the data if the data the user entered is not a duplicate.
Here's a sample of my code:
using (SqlConnection conn = new SqlConnection(ConnStr))
{
string Command = "SELECT CountryName FROM [Countries] WHERE CountryName = @Name";
using (SqlCommand comm = new SqlCommand(Command, conn))
{
comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar, 20);
comm.Parameters["@Name"].Value = Name;
comm.Parameters.Add("@IsVisible", System.Data.SqlDbType.Bit);
comm.Parameters["@IsVisible"].Value = IsVisible;
conn.Open();
if (comm.ExecuteScalar() == null)
{
Command = "INSERT INTO [Countries] (CountryName, IsVisible) VALUES (@Name, @IsVisible);";
comm.ExecuteNonQuery();
}
}
I was trying to save a trip to the database by using one connection.
The Problem is:
The first command runs okay but the second command which inserts into the database won't work (it doesn't add any records to the db) and when I tried to display the rows affected it gave me -1 !!
The Question is:
Is this is the ideal way to check for a duplicate records to constraint a unique country ? and why the second command is not executing ?
View 4 Replies
Similar Messages:
Jan 6, 2010
I want to issue an INSERT command for an SQL Server table using DataAdapter without first issuing a SELECT command. Could anybody send me lines of code to handle this? Also how do i manage INSERT into selective table columns (I have 10 columns but i only want to update 2 of them)?
View 2 Replies
Jun 3, 2013
I am working with odbcCommand class, in one case I got error that QueryTimeout Expired. Even though this SP is taking only 3-4 secs in DB to execute these specific values, When I set the CommandTimeout=0, then it worked fine.
1)Is it necessary to always use this property while working with Command Class.
2)If it is not suggested to use, but still if I use it then how it will impact the performance.
Below is my code sample.
OdbcConnection conObj;
OdbcCommand cmdObj;
OdbcDataAdapter daObj = new OdbcDataAdapter();
public DataTable GetIFAContractNoteData(string RecipientIDIFACN, DateTime BatchDateIFACN, int TransmittalReportIDIFACN) {
conObj = new OdbcConnection(GlobalVariables.strDsnName + ";" + GlobalVariables.strDsnDataBase + ";" + GlobalVariables.strDsnUserID + ";" + GlobalVariables.strDsnPassword);
[code]....
View 1 Replies
Aug 3, 2010
How do i run a shell command in asp.net ?
I basically want something like system("netstat -an");
I don't want the output to be displayed to the user. Just want to run some maintainence commands ...
View 2 Replies
Sep 2, 2010
[Code]....
Using above code, I got succeeded in printing PDF file through command line. But it runs the Acrobat Reader and opens Save dialog box.
In my case, I want to suppress Save dialog box and save the file on other location using c# coding. I mean I want to save a PDF file behind the scene.
View 1 Replies
Feb 3, 2011
I'm in process to create a few utilities for my team to make life a bit easier working with our Unix boxes(most of them Solaris based).
For example I'm creating a ASP .NET page to display the output of TOP. Also plan to be able to restart processes with the KILL -15 command.
Now I wonder if there is any nice modules out the do the work for me or am I better off just going ahead with my own SSH communication?
It would of course make sense building the app on the unix box directly but I'm not able to do this.
View 1 Replies
Feb 19, 2011
Is there a way that I can call a function like (fadeIn or fadeOut) from the .cs?So thats my situation - I have a datalist of stuff. When A user clicks something on the list, it gives him some details about the thing he clicked and if he wants it, he clicks it again.now, on the first click I changed the OnClientClick of the button to fadeOut the datalist.so on the second click (after he confirms the item) it fades out.If he clicks one thing than click another thing, the fadeOut occurs.
View 4 Replies
Sep 20, 2010
I have a simple gridview. I can view the data and I have gotten the insert to work (code behind).
Code:
Dim vRepairId As String = Request.QueryString("REPAIRID")
SqlDS_WOAccount.InsertParameters("WorkOrderID").DefaultValue = vRepairId
SqlDS_WOAccount.Insert()
<asp:GridView ID="GridViewAccount" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDS_WOAccount" Font-Size="8pt" Width="100%">
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
[code]...
View 31 Replies
Mar 8, 2011
I have web.config file and it is encrypted completely and hosted in one server,Now I got one requirement to change admin person mail id. So I need to decrypt the web.config file and update the mail id then encrypt the config file again. Please tell me how to do this using aspnet_regiis -pdf commands?
View 1 Replies
Feb 9, 2011
I stripped this example to make it simple. I have a gridview with a template field. The template field contains two buttons and a label (They must be in the same template field). I want the first button to set the label text to "Win", and the other button to set the label text to "fail". The onrowcommand doesnt seem to be triggered by buttons in a template field. How can I accomplish this?
My gridview code is below:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AutoGenerateColumns="False"
OnRowCommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField ShowHeader="False">
[Code]....
View 2 Replies
Mar 25, 2011
Below is my SQL query in which I have used UNION All In my outer query.so I want to select data onwards particulardate. [code]...
View 1 Replies
Mar 16, 2012
In a gridview, how does one create/add more than one button where each does something different?
I need 3 buttons for each row in a gridview.
Each will open a different target page/url when clicked on. I've done this in classic asp but how does one do it in asp.net using gridview? Or should I be doing something else? Maybe a repeater or some other control? I would need sorting and paging too.
I am trying to duplicate the functionality of this classic asp application I created years ago:
[URL]
Search for street name="oaK"
The results of the search is what I need to duplicate in asp.net.
I have the basics done but can't figure out how to make additional buttons in the gridview of the results do different things in this asp.net application:
[URL]
View 3 Replies
Aug 19, 2010
I've recently started working in ASP.NET and RDBMS.I'm making a test form where I can do basic add,update,delete operations.I'm using OracleDataClient to do the DB operations.I was able to populate a listbox using OracleDataAdapter.
Now after clicking on update button,I intend to Update in DB.I've Dataadapter with it's update property.But the update query is not happening.The examples I saw over net all have Select command before Update.Is it actually like that or am I missing some point.
How does Oracle DataClient work with Insert,Update,Delete Commands.
View 1 Replies
Jun 24, 2012
I am executing Sql Command of insert
But after it inserts data into table it refreshes page and on refreshing, it is calling my javascript window.onunload event
Can we Execute sql command without refreshing page???
View 1 Replies
Apr 25, 2010
Someone developed nice HTML pages for a new site. My assignment is to add the code to make the site working with SQLServer.
I know how to make a new site starting from VWD or VS2008 c#
What would be the best approach? Building a new site and afterwards trying to swap my HTML with the one provided? Or what?
View 3 Replies
Jul 14, 2010
I have a problem with listview and I can't find any useful solution.
Here is my situation:
I have a listview with insert row and i need to do some calculations as user types in. Some fields are not filled by user but computed by javascript function (they are not inserted into db, they are shown for informational purposes only). These fields are just textboxes - computed ones are disabled. So i want to add some javascript functions to handle the computation, but i need this functions to have proper IDs. So i add them dynamically on listview.itemcreated with attributes.add. And that's where the problem begins.
If I use textbox.attributes.add("onblur", "some code"), suddenly all row commands of insert row stop working. They seem to cause a postback (all filled fields are blank after) but they are not firing any event. ItemCommand, ItemInserted, ItemInserting... Nothing. I have a masterpage and updatepanel in my website but i don't think that is the source of all problems. If I clear all javascript events from all textboxes and dropdownlists, row commands are working again. But I really need some client side computation and validation and I don't know how to make it work and still can insert rows.
View 1 Replies
May 14, 2010
I am trying to write an app that has one gridview, but I change the select on my sql data source to show different information on the same grid. For example I start showing units, but when price is selected I reassign my select command from Select sales.[sales1] to Select (sales.[price] * sales.[sales1]) as sales1.
However, this is causing a problem that I cannot understand with the built-in edit functionality of gridview. Whenever I click on edit, all the data in my gridview reverts back to the original select and displays only units. Am I missing something? When I change my select command the only things I am changing are my gridview.datakeynames, sqlsource.selectcommand, and sqlsource.updatecommand. What do I need to add to make my gridview edit using the current selectcommand?
View 3 Replies
Apr 24, 2010
Is it possible to declare custom commands within a listview control? For example, if I want to have a "Pay Now" link for each item in the listview, how would I do that?
View 2 Replies
Feb 13, 2010
I plan on having multiple Gridviews on one page populated completely in code behind. The only difference between them will be the Stored Procedure. In an effort to save having to write the following for each gridview, can I reuse the commands for each of them. Below is a sample of one of the commands that I would like to use, as well as the bind sub that I am using.
I realize that it will not be at all difficult to cut and paste each of these lines, and making the minor edits for each one. I just would like to keep my code behind file thin, as well as only limiting the chance for mistakes by using multiple commands.
[Code]....
View 7 Replies
Nov 26, 2010
I want to execute some powershell commands on a remote windows 2008 machine. Right now i am using psexec to run the powershell from c# asp.net application. I would like to know is there any way to execute powershell commands without using a third party tool like psexec?
View 1 Replies
Feb 19, 2010
how do i edit and delete in gridview without sqldatasource commands?
View 2 Replies
Apr 9, 2010
i'm trying to issue a select command in my app soi could execute the command and retrive the data into a string variable.the problem is that this command is overloaded with DataSourceSelectArgument and i can't figure out what it is.i'm using sql server express and when issue a n insert command for example sq.Insert(); i have no problems.this the command that's holding me:
SqlDataSource sq = new SqlDataSource();
sq.ConnectionString = ConfigurationManager.ConnectionStrings["CustomerDatabaseConnectionString1"].ToString();
sq.SelectCommandType = SqlDataSourceCommandType.Text;
sq.SelectCommand = "SELECT * FROM CustomerTable where customerID = 1";
string result = sq.Select(some overload that's stopping me);
View 5 Replies
Apr 26, 2010
how would i do my Gridview delete command like a update command?
im using a datasource... and my delete command is like my update command... so i can change the status into "INACTIVE"...
DeleteCommand="UPDATE ACCOUNT_MAINTENANCE SET am_status = 'INACTIVE' WHERE (accmain_no = @accmain_no)"
UpdateCommand="UPDATE [ACCOUNT_MAINTENANCE] SET [account_type] = UPPER(@account_type), [min_deposit] = @min_deposit, [min_capital] = @min_capital WHERE [accmain_no] = @accmain_no"
when i press my delete command at gridview... the status changed into "INACTIVE" but when i refresh or press f5... it automatically update the status into "INACTIVE" also...
View 3 Replies
Sep 30, 2010
I have a datagridview that has a column which displays a ref value from another unbound table, but during an edit of a gridview entry i want a dropdownlistbox to list all the available ref values from the other unbound table source, then to insert the selected value into the bound table
View 1 Replies
Nov 16, 2010
I want to be able to show the record that was just inserted after add_button_click event. I run the insert from mycommand and then set mycommand2 to run the read to populate a details view. I have a feeling I am going about this the wrong way. The insert fires correctly but the details view does not populate.
[Code]....
View 3 Replies