|
Firstly, that's not how you use Parameterised queries:
string cmd_query = "UPDATE [book_tbl] SET [Type_ID] = '@cmbtyp', ..."; Does not set the Type_ID column to the value of cmbtype.Text it sets it to a litteral string "@cmbtype". To set a parameter value, you need to get rid of the quote marks:
string cmd_query = "UPDATE [book_tbl] SET [Type_ID] = @cmbtyp, ..."; In addition, you don't need square brackets round every name, just teh ones that contain spaces, or which conflict with SQL keywords:
string cmd_query = "UPDATE book_tbl SET Type_ID = @cmbtyp, ...";
Secondly, AddWithValue doesn't have an override that takes a Type: so the value it initially sets is the value of the enum, not the value you think.
Try this:
cmd.Parameters.AddWithValue("@isbn", txtISBN.Text);
Thirdly, an ISBN is not an integer! The max value in an SQL INT is 2,147,483,647, which is only 10 digits, and from 2007 they are always 13 digits.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi again, thanks for input, I did as you advised but now error is " Error must declare scalar variable @cmbtyp".
string cmd_query = "UPDATE book_tbl SET Type_ID = @cmbtyp,category =@cmbcat,title =@title,publisher_ID =@cmbpub, [A_ID] =@cmbauthor,first_name=@fname,last_name=@lname,publication =@pub WHERE ISBN=@isbn ";
cmd.Parameters.AddWithValue("@cmbtyp", cmbtyp.Text);
cmd.Parameters.AddWithValue("@cmbcat", cmbcat.Text) ;
cmd.Parameters.AddWithValue("@title", txtTitle.Text);
cmd.Parameters.AddWithValue("@cmbpub", cmbpub.Text) ;
cmd.Parameters.AddWithValue("@cmbauthor", cmbauthor.Text);
cmd.Parameters.AddWithValue("@fname",txtfname.Text );
cmd.Parameters.AddWithValue("@lname", txtlname.Text);
cmd.Parameters.AddWithValue("@pub",txtpub.Text ) ;
cmd.Parameters.AddWithValue("@isbn", txtISBN.Text);
con = new SqlConnection(ConnectionString);
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmd_query;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("record updated successfully");
clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
|
|
|
|
|
That's because you are adding the parameters to the previous cmd instance - you create the new SqlCommand object after you add the parameters and their values.
Try this:
using (con = new SqlConnection(ConnectionString))
{
con.Open();
string cmd_query = "UPDATE book_tbl SET Type_ID = @cmbtyp, category = @cmbcat, title = @title, publisher_ID = @cmbpub, A_ID = @cmbauthor, first_name = @fname, last_name = @lname, publication = @pub WHERE ISBN = @isbn";
using (cmd = new SqlCommand(cmd_query, con))
{
cmd.Parameters.AddWithValue("@cmbtyp", cmbtyp.Text);
cmd.Parameters.AddWithValue("@cmbcat", cmbcat.Text) ;
cmd.Parameters.AddWithValue("@title", txtTitle.Text);
cmd.Parameters.AddWithValue("@cmbpub", cmbpub.Text) ;
cmd.Parameters.AddWithValue("@cmbauthor", cmbauthor.Text);
cmd.Parameters.AddWithValue("@fname",txtfname.Text );
cmd.Parameters.AddWithValue("@lname", txtlname.Text);
cmd.Parameters.AddWithValue("@pub",txtpub.Text ) ;
cmd.Parameters.AddWithValue("@isbn", txtISBN.Text);
cmd.ExecuteNonQuery();
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Do NOT use AddWithValue , use Add method instead. The syntax you are using suggests that Add had been used at first, and that you changed it to AddWithValue without taking care of parameters.
AddWithValue can cause issues because it has to infer database types from inputs, whereas Add method allows specifying database type.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
Hi,
I am trying to use ESENTUTL to copy the WebCacheV01 file. Running from the command line it works a treat, but trying to run it using C# code, it just won't have it.
OutputDirectoryCheckCreate();
using (var cmdEsentutl = new Process())
{
cmdEsentutl.StartInfo.FileName = "esentutl";
cmdEsentutl.StartInfo.UseShellExecute = false;
cmdEsentutl.StartInfo.CreateNoWindow = false;
cmdEsentutl.StartInfo.Arguments = "/y /vss " + GlobalVariables.edge44HistoryDB + "WebCacheV01.dat /d " + "F:\\WebCacheV01.dat";
cmdEsentutl.StartInfo.RedirectStandardError = true;
cmdEsentutl.Start();
cmdEsentutl.WaitForExit();
}
Anybody have any clue where I am going wrong?? The application has already gained admin rights when it launches.
Thanks
Colin
|
|
|
|
|
Have you tried using the debugger to look at exactly what is in the StartInfo.Arguments field when it reaches the Start instruction?
At a guess - and without seeing your screen that's the best I can do ATM - it's not the same as the one you are using directly to CMD.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
Quote: it just won't have it doesn't tell us a lot. Is the process getting launched and refusing its command line arguments, not launched at all, or what?
I suggest you put all code shown in a single and permanent try-catch block that somehow shows any potential exception in its integrality, i.e. using Exception.ToString()
As a wild guess, you might be missing a space right after edge44HistoryDB .
|
|
|
|
|
POSSIBLY, since I know nothing of ESENTUTL, your command line, as built, is bad. You're using string concatenation to build the command line and that's a bad way to do it, prone to bugs.
Run the code under the debugger and put a breakpoint on the line:
cmdEsentutl.StartInfo.RedirectStandardError = true;
When the debugger stops on the breakpoint, hover the mouse over the 'Arguments' portion of this line:
cmdEsentutl.StartInfo.Arguments = "/y /vss " + GlobalVariables.edge44HistoryDB + "WebCacheV01.dat /d " + "F:\\WebCacheV01.dat";
This will show you the arguments you're passing on the command line. You don't need the extra "+" near the end of the line. That just makes the code more error prone and harder to debug. Also, chances are really high you don't have a space character where there should be one for the command line to make sense, like immediately before 'WebCacheV01.dat...'. That line of code should probably looks more like this:
cmdEsentutl.StartInfo.Arguments = "/y /vss " + GlobalVariables.edge44HistoryDB + " WebCacheV01.dat /d F:\\WebCacheV01.dat";
Even better, use string interpolation to build the arguments instead:
cmdEsentutl.StartInfo.Arguments = $@"/y /vss {GlobalVariables.edge44HistoryDB} WebCacheV01.dat /d F:\WebCacheV01.dat";
|
|
|
|
|
Thanks for your help guys.
I have altered the code slightly and taken into account some of the suggestions.
{
cmdEsentutl.StartInfo.FileName = "esentutl";
cmdEsentutl.StartInfo.UseShellExecute = false;
cmdEsentutl.StartInfo.CreateNoWindow = false;
cmdEsentutl.StartInfo.Arguments = $@"/y /vss {GlobalVariables.edge44HistoryDB}\WebCacheV01.dat /d E:\WebCacheV01.dat";
cmdEsentutl.StartInfo.RedirectStandardError = true;
cmdEsentutl.Start();
cmdEsentutl.WaitForExit();
}
I have checked the error log and it does start the esentutl.exe, which it wasn't before. I am now getting an error on running, rather than a code error.
Extensible Storage Engine Utilities for Microsoft(R) Windows(R)
Version 10.0
Copyright (C) Microsoft Corporation. All Rights Reserved.
Initializing VSS subsystem...
VSS Subsystem Init failed, 0x80042302
Operation terminated with error -2403 (JET_errOSSnapshotNotAllowed, OS Shadow copy not allowed (backup or recovery in progress)) after 0.172 seconds.
Thanks everyone.
|
|
|
|
|
Is your application running elevated? It looks like you need admin privileges to copy this file.
The internet suggests ( http://blogs.msdn.com/b/martinc/archive/2015/02/11/using-esentutl-exe-vss-to-examine-an-in-use-database.aspx[^] ) to make a Volume Shadow Service copy in order to read the database. Doing this from a normal user level produces:
esentutl /mh WebCacheV01.dat /vss
VSS Subsystem Init failed, 0x80070005
Operation terminated with error -2403 (JET_errOSSnapshotNotAllowed, OS Shadow copy not allowed (backup or recovery in progress)) after 0.15 seconds.
Reading:
http://blog.nirsoft.net/2012/12/08/a-few-words-about-the-cache-history-on-internet-explorer-10/[^]
I tried again from a cmd prompt with admin privileges. This worked! Dumping table information failed because the database was not closed (or something, I didn't save the error message).
I ended up using the following magic incantation:
esentutl /y WebCacheV01.dat /vssrec V01 . /d destination\path\for\copy.dat
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks
I have updated my code to run with admin privileges, but it still doesn't copy out. If I do it manually from the command prompt, it works fine.
using (var cmdEsentutl = new Process())
{
cmdEsentutl.StartInfo.FileName = "esentutl";
cmdEsentutl.StartInfo.UseShellExecute = false;
cmdEsentutl.StartInfo.CreateNoWindow = false;
cmdEsentutl.StartInfo.Arguments = $@"/y {GlobalVariables.edge44HistoryDB}\WebCacheV01.dat /vssrec V01 . /d F:\WebCacheV01.dat";
cmdEsentutl.StartInfo.Verb = "runas";
cmdEsentutl.StartInfo.RedirectStandardError = true;
cmdEsentutl.Start();
cmdEsentutl.WaitForExit();
}
|
|
|
|
|
You can't combine Verb = "runas" with UseShellExecute = false - the process will just launch without elevating.
That means you'll need to set RedirectStandardError = false as well. Which is a good thing - you wouldn't want unprivileged applications being able to read the output of applications running as administrator, because that would be a serious security vulnerability.
The only other option is to make the calling application (your code) run elevated.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Maybe late answer.
You might need to set the cmdExentutl.StartInfo.WorkingDirectory property. I'm not sure what working directory you should use, but it might be the to the same folder as the F:\\WebChacheV01.dat file. Just set the property before the cmdExentutle.Start () call.
I have had weird errors when passing a file as the argument for a process and missed the working directory. Without passing the working directory, the process didn't knew where to locate the file specified as an argument, even if I specified the absolute file path.
|
|
|
|
|
Dear Rossi,
Using your pop filter then datagridview color changed. I want to filter without change color.plz help to do the above request
|
|
|
|
|
It looks as though you are asking a question about a piece of code in an article. At the end of the article is a forum for you to ask questions. That's where you should ask your questions, not here. The chances of the author just happening by and seeing the question are very slim.
|
|
|
|
|
Don't post this here - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi as i am a beginner i am currently doing a project alone in c# and it describes that
1) To retrieve the senders name, mail address,subject and body of an unread email in outlook.
2) To add a buttons in a context menu that when we right click on a unread email we need to get a button called `spam` and `not spam` and we should write a javascript code when we click on that spam button it the mail should move to spambox. please can anyone help me out in getting this?
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
The first part to retrieve the senders name, mailid and body of the message i tried using the following code
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
mapiNameSpace.Logon(null, null, false, false);
mapiNameSpace.Logon("YourOutlookMailID", "Password", Missing.Value, true);
Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);
mapiNameSpace.Logon("Profile", Missing.Value, false, true);
if (myInbox.Items.Count > 0)
{
Console.WriteLine(string.Format("Total Unread message {0}:", inboxItems.Count));
int x=0;
foreach (Microsoft.Office.Interop.Outlook.MailItem item in inboxItems)
{
Console.WriteLine("{0} unread mail from your inbox", ++x);
Console.WriteLine(string.Format("from:- {0}", item.SenderName));
Console.WriteLine(string.Format("To:- {0}", item.ReceivedByName));
Console.WriteLine(string.Format("Subject:- {0}", item.Subject));
Console.WriteLine(string.Format("Message:- {0}", item.Body));
System.Threading.Thread.Sleep(1000);
}
Console.ReadKey();
}
[edit]Code block added - OriginalGriff[/edit]
modified 25-Feb-20 4:15am.
|
|
|
|
|
And?
Does it work? How did you test it? What results did you get?
What have you tried?
Where are you stuck?
What help do you need?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ya it worked fine at some part as when we test it in VSTO it redirects and opens outlook. But i am not finding any output as it is supposed to display the retrieved data when we test.
|
|
|
|
|
So what have you done to find out why? What does the debugger show?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I need help to fix an issue where when data from web application is exported to Excel by clicking a button export to excel, if the data in a cell contains double quotes, that data should be displayed without the double quotes visible.
Previously I made a change to the application code in VB so that the output exports text fields with formulas (="") to force Excel to treat those values as a string. This has been working except some instances where the output actually displays the formula characters (="") within the cell as text, rather than as hidden formulas. It appears when a cell contains text with an actual double quotes that is when after export to Excel is done, those quotes appear in Excel. I need help to figure out if there is a way to suppress those.
For example. A cell with the following data Allows the user the abilities to Add, View, Modify and Delete Notes on the Notes Tab of the Case Record. "View" allows the user to view the Notes Tab of the Case Record.
When this is exported to Excel the data is displayed as follows ="Allows the user the abilities to Add, View, Modify and Delete Notes on the Notes Tab of the Case Record. "View" allows the user to view the Notes Tab of the Case Record. I do not want to quotes to appear in Excel.
On the other hand, a cell with the following data Maintain Victim Classification Types. when this is exported to Excel there are no visible quotes. It displays as Maintain Victim Classification Types.
Here is my VB code that needed changing
Dim row As DataRow
For Each row In dt.Rows
sw.Write(vbNewLine)
Dim column As New DataColumn()
For Each column In dt.Columns
If Not row(column.ColumnName) Is Nothing Then
sw.Write("=""" & row(column).ToString().Trim() & """" & vbTab)
Else
sw.Write(String.Empty + vbTab)
End If
Next column
Next row
modified 24-Feb-20 14:44pm.
|
|
|
|
|
NB: You've posted this in the C# forum. It should really be in the VB.NET forum, since you're using VB.NET code.
You're exporting to a "tab-separated variables" file. You will have very limited control over formatting for this type of file.
It would probably be better to export to a "real" Excel file instead. For example, using EPPlus[^]:
Using package As New ExcelPackage()
Dim sheet As ExcelWorksheet = package.Workbook.Worksheets.Add("Sheet1")
sheet.Cells("A1").LoadFromDataTable(dt, True, TableStyles.Medium9)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
package.SaveAs(Response.OutputStream)
End Using EPPlus will give you a lot more control over the resulting file:
Getting Started · JanKallman/EPPlus Wiki · GitHub[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I think what you are asking is how to have double quotes inside a string that is being converted into a quoted string. If so, not only do you need to add double quotes at both ends (which you already do), but you ned to double the embedded double quotes, viz:
sw.Write("=""" & Replace(row(column).ToString().Trim(), """", """""") & """" & vbTab)
(It looks yucky because the double quotes have to also be doubled in the 'from' and 'to' arguments in the Replace() function)
|
|
|
|
|