|
Member 10388494 wrote: I didn't mean it in a bad way
Yes you did. What you said about "lazy" has no other interpretation.
Member 10388494 wrote: I am just trying to find how to write in database without the DataAdapter
WHY? What you're doing is exactly what the DataAdapter was designed to do! Ok, fine. While you're sitting there re-inventing the DataAdapter, the rest of us "lazy" people will use the DataAdapter, be done with the code and have moved on to other parts of the project completing them before you do.
|
|
|
|
|
Hello. I have this code to update A RECORD IN sql server . But it's giving me an error:
wrong syntax near Where.
Any help Please?
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim strconnection As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=DatabaseConnection;Integrated Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
Dim cmd As New SqlCommand
_cn.Open()
Dim strsql As String
strsql = "update tblcustomer set "
strsql = strsql & " ID = '" & txtCurrent.Text & "', "
strsql = strsql & " name = '" & txtName.Text & "', "
strsql = strsql & " email = '" & txtEmail.Text & "', "
strsql = strsql & " address = '" & txtAddress.Text & "', "
strsql = strsql & " deptid = " & cbDept.SelectedValue
strsql = strsql & " where serial = " & Val(txtserial.Text)
cmd.CommandText = strsql
cmd.Connection = _cn
cmd.ExecuteNonQuery()
cmdRequery.PerformClick()
End Sub
serial is autoincremented in sql server.
|
|
|
|
|
You really shouldn't build your SQL like that. If I were you, I would consider using parameterized queries so that you can avoid SQL Injection attacks.
|
|
|
|
|
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim strconnection As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=DatabaseConnection;Integrated Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
Dim cmd As New SqlCommand
_cn.Open()
cmd.CommandText = "update tblcustomer set ([ID],[Name],[Email], [Address], [DeptID]) VALUES (<a href="/Members/id">@ID</a>, <a href="/Members/Name">@Name</a>, <a href="/Members/email">@Email</a>, <a href="/Members/address">@Address</a>, @DeptID)"
cmd.Connection = _cn
cmd.Parameters.AddWithValue("<a href="/Members/id">@ID</a>", Me.txtID.Text)
cmd.Parameters.AddWithValue("<a href="/Members/Name">@Name</a>", Me.txtName.Text)
cmd.Parameters.AddWithValue("<a href="/Members/email">@Email</a>", Me.txtEmail.Text)
cmd.Parameters.AddWithValue("<a href="/Members/address">@Address</a>", Me.txtAddress.Text)
cmd.Parameters.AddWithValue("@DeptID", Me.cbDept.SelectedValue)
txtserial.Text = cmd.ExecuteScalar()
End Sub
I wrote this one now. But it's giving me error on the executescalar. syntax error near )
Am i missing something?
And if i commented this line of code:
txtserial.Text = cmd.ExecuteScalar()
when i edit the textboxes , everything looks normal. i go next-previous-move last-move first. everything is fine. but not the database.
the edited record stays as it was before editing.
Any idea why?
Thank you
|
|
|
|
|
You shouldn't be executing a scalar - that's expecting a value back. Use ExecuteNonQuery instead.
|
|
|
|
|
I tried.
Incorrect syntax near )
but i need the executescalar because i need the first columns value to be in txtserial
|
|
|
|
|
The problem that you have here is that you are performing an update. If you execute the command that you have there, you will update EVERY record because you have no where clause. What are you trying to do here?
|
|
|
|
|
You're mixing the syntax for the INSERT [^] and UPDATE [^] statements.
If you want to insert a new record, the query should be:
INSERT INTO tblcustomer
(
[ID],
[Name],
[Email],
[Address],
[DeptID]
)
VALUES
(
@ID,
@Name,
@Email,
@Address,
@DeptID
)
If you want to update an existing record, the query should be:
UPDATE
tblcustomer
SET
ID = @ID,
Name = @Name,
Email = @Email,
Address = @Address,
DeptID = @DeptID
WHERE
Serial = @Serial
(You would also need to add the @Serial parameter to the command.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes you are right Richard Deeming.
It worked. the databse is updating . and that's the important thing.
Thanks a lot guys.
But once i put this line of code:
txtserial.Text = cmd.ExecuteScalar.ToString()
an eror pops up:
Object reference not set to an instance of an object.
And i need the first columns value to be in the txtserial texbox.
Any advice?
|
|
|
|
|
You're getting a NullReferenceException because you aren't returning anything. You're either passing in the serial number for an update statement (in which case you don't need to get it out), or you should be trying to SELECT SCOPE_IDENTITY after doing your insert to get the value of the record you inserted.
|
|
|
|
|
Pete O'Hanlon wrote: SELECT IDENTITY_SCOPE()
Or SCOPE_IDENTITY() as it's known!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's only in those ol' versions of SQL Server. I'm running SQL Server 2432.
|
|
|
|
|
|
You will also get problems with this syntax if one of the text values includes an apostrophe, e.g. a name like "O'Connor" or "D'artagnan"
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Chris Quinn
No. I just tried it. It's working
|
|
|
|
|
Hello,
I'm using VS2010 with .Net 4.0 for a solution with numbers of ClassLibrary projects. One projet needs 4.5 functionnality so I've compiled it in VS2012 targetting .Net 4.5.
In my VS2010 solution, I can add references to the MyLibrary4.5.dll, but I can't use it (namespace unknown)
Can't we do that ?
|
|
|
|
|
Not in VS2010, no. That version of Visual Studio was released with .NET 4.
|
|
|
|
|
No. You cannot add a new dll to an older version of the framework.
|
|
|
|
|
I am using Google Auth api Calendar in .net. i am facing the same error in every Google apis code. And i donot know know ho to remove it. please HELP
My Error is: "Could not load type 'Google.Apis.Requests.IErrorResponseHandler' from assembly 'Google.Apis, Version=1.6.0.16897, Culture=neutral, PublicKeyToken=null'"
public ActionResult EmailSettings(string jsonData, string tokenAuthenticate, string clientID, string APIkey, string ClientSecret)
{
HttpContext.Application["EmailAdmin"] = jsonData;
HttpContext.Application["EmailAccessOken"] = tokenAuthenticate;
this.clientID = clientID;
this.APIkey = APIkey;
this.ClientSecret = ClientSecret;
CreateService(tokenAuthenticate);
return View();
}
private CalendarService CreateService(string token)
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, this.clientID, this.ClientSecret);
var auth = new Google.Apis.Authentication.OAuth2.OAuth2Authenticator<NativeApplicationClient>(provider, (p) => GetAuthorization(provider, token));
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApiKey = ConfigurationManager.AppSettings[this.APIkey].ToString(),
GZipEnabled = false
});
return service;
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg, String Refreshtoken)
{
IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
state.RefreshToken = Refreshtoken;
return state;
}
modified 12-Dec-13 10:44am.
|
|
|
|
|
Please don't attempt to take people from this site to StackOverflow. That's not a very courteous thing to do.
|
|
|
|
|
Agree with Pete. If you want answer, mention all details clearly in your question. Besides some websites blocked on some workplaces(very rare thing). So update your question with complete details clearly.
thatrajaCode converters | Education Needed
No thanks, I am all stocked up. - Luc Pattyn
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute
|
|
|
|
|
|
Member 10457223 wrote: What should i put here: //PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt//
what path?? Your report Path(Where you have report files). For example, if you have your report files in some folder on D Drive, then code should be like below.
cryRpt.Load(@"D:\Somefolder\CrystalReport1.rpt")
But don't use hard code path, use Relative path . C# get project path[^]
thatrajaCode converters | Education Needed
No thanks, I am all stocked up. - Luc Pattyn
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute
|
|
|
|
|
I need to simulate keyboard and mouse actions in a simple C# Visual Studio Windows Application. Some years back, it was relatively simple to do this, as I recall. (Microsoft once even had a whole programming environment called MSTest built around this functionality.) Now, the only way that I see to do it within Visual Studio is with a Hyper-V WMI class. I do not see what library those classes are in, and I wonder if there is a simpler way anyway.
W. Johnson
|
|
|
|
|
What you are looking for is something called Automation Peers. You can find some information on this technology here[^], and using it in WPF here[^].
|
|
|
|