|
Two obvious errors that I can see:
1. You are risking the integrity of your database by using string concatenation for your queries.
2. You create a new form in btnMainmenu_Click , but then lose it as its reference goes out of scope.
I suggest you fix those two problems first, and then see what happens.
|
|
|
|
|
You create a Sql-connection without ever setting the connection-string. Add a con.ConnectionString = "blablabla" before opening the connection.
Member 13683765 wrote: cmd.CommandText = "insert into tblBook (Book_ID, Name, Authour, Category, Donater_ID, Section_ID) values ('" + txtBoxBookId.Text + "', '" + txtBoxBookName.Text + "', '" + txtBoxAuthour.Text + "', '" + txtBoxCategory.Text + "', '" + txtBoxDonaterId.Text + "', '" + txtBoxSecId.Text + "')"; That's an abomination. It is unsafe as mentioned by Richard, due to SQL-injection. It is hard to update when adding a column and it will become a annoying thing to read if the string becomes long enough that you have to scroll in the IDE.
try
{
string MyConnectionStringHere = "";
using (var con = new SqlConnection(MyConnectionStringHere))
using (var cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = @"
INSERT INTO tblBook (
Book_ID,
Name,
Authour,
Category,
Donater_ID,
Section_ID
) VALUES (
@Book_ID,
@Name,
@Authour,
@Category,
@Donater_ID,
@Section_ID)";
cmd.Parameters.AddWithValue("@Book_ID", txtBoxBookId.Text);
cmd.Parameters.AddWithValue("@Name", txtBoxBookName.Text);
cmd.Parameters.AddWithValue("@Authour", txtBoxAuthour.Text);
cmd.Parameters.AddWithValue("@Category", txtBoxCategory.Text);
cmd.Parameters.AddWithValue("@Donater_ID", txtBoxDonaterId.Text);
cmd.Parameters.AddWithValue("@Section_ID", txtBoxSecId.Text);
if (1 == cmd.ExecuteNonQuery())
{
}
else
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
A few notes;
- You'll still need to set a connection-string in the example, but you don't need SqlConnections in multiple methods, or as a member-variable. It will be closed when exiting the "using" statement.
- You want the complete exception-text; it will tell you where and more.
- Those column-names using up many lines may seem excessive to you; go stand on such a line in the IDE and press Ctrl-X. Move the cursor down two strokes and press Ctrl-V.
Good luck
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
What is the difference between unit tests, integration tests, acceptance tests, functional tests and regression tests?
|
|
|
|
|
|
They test different things; like a tire-test, a motor-test and a customer-satisfaction-survey to a car-salesman.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Up-voted for possible mix of analogy and irony.
I fear the OP here may be headed for an emissions-test fail in the job interview.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
You think he'll blow it? Exhausting
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Until you put in some effort to educate yourself by doing some basic research, there will be no difference.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
How to create sale invoice and print using datagridview values in c# windows applications.
Please help me to find out this solution. Thanks.
|
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
And so far, we have no idea of what you have tried, where you are stuck, or what help you might need.
Since we aren't in the business of doing all your work for free, we aren't going to guess and write you a complete app!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm not "brand new" to C# and so I'm still struggling with some concepts. All of the projects I've created so far have a Globals.cs class. It's a class without a namespace and so I use it to hold just a few configuration settings that I want to apply to every file in my project.
I've recently been told that I should NEVER use what he called, top level properties. They aren't read only so they can't be considered constants.
Is there a better way to handle this?
Maybe it's common practice to handle this with a config class?
Maybe I should be using abstraction?
Any advice would be awesome
Thanks for your help!
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
That had a few things I have no idea about
I did not know you could even create a class without a namespace
What is a top level property google did not help!
What would you want to apply to EVERY (presumably .cs) file in a project.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Some folks hate static anything; some folks hate the idea of a Class "floating" outside any NameSpace; pundits rail against both as "violations" of OOP.
.Net itself often glues a bunch of static methods in a Type definition, like with 'String.
But, fact is: .NET C# provides a facility to allow a Class to be declared outside any user-defined NameSpace and used as a repository accessible to all app entities (when declared 'static, of course). This "floating Class" will exist in the Global Application NameSpace.
Whether you should use this facility should be, imho, a result of your choice based on your knowledge of .NET facilities for saving state of the app, of the app's controls, of the data the app manages. And, possible threading issues if the app is multi-user.Rick_Bishop wrote: They aren't read only you can make its contents read-only:
using System;
using System.IO;
public static class Globals
{
public const Int32 MaxFormWidth = 1000;
public static String SerializeFileName { set; get; } = @"Data.xml";
static readonly String SerializeFolderPath = @"MyAppSavedData";
public static string GetSerializeFilePath(string filename = "")
{
if (filename != "" && SerializeFileName != filename)
{
SerializeFileName = filename;
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
path = Path.Combine(path, SerializeFolderPath, SerializeFileName);
if(IsValidPath(path)) return path;
throw new FileLoadException($"{path} is not a valid file path");
}
static bool IsValidPath(string path)
{
}
} 'MaxFormWidth is readonly, 'SerializeFileName is read/write, 'SerializeFolderPath is read-only.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
modified 19-Feb-18 3:45am.
|
|
|
|
|
Thanks for the insight! This line of code:
public static String SerializeFileName { set; get; } = @"Data.xml"; Is that setting a default value on SerializeFileName? I didn't know you could do that. Wow. Thank you. I always thought you had to do that in a backing field.
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
It's a newer syntax for initializing an auto-property available with C# 6: [^].
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
That's really nice. I love when I learn new things like that. Thanks, Bill!
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
Thank you - I learn lots every day.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
A minor correction; you cannot have a class outside a namespace. If you fail to define one, your class will automatically be added to the global namespace[^].
This space for rent
|
|
|
|
|
A good point, Pete; I will revise my post to say: "outside a user-defined NameSpace."
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hi Team,
Need your help on Copy/Paste on DateTimePicker.
I am displaying the UI in DD/MM/YY format and when I copy it copies in the same format(DD/MM/YY).
But Paste uses MM/DD/YY format.
How can i change the Paste format to DD/MM/YY?
Example UI is displaying 16/02/18(which is todays date in DD/MM/YY) and when I copy it copies as it is 16/12/18.
Now I modify it as tomorrows date 17/12/18 I does not allow as it validates for MM/DD/YY and 17 is greater then 12 so it does not copy.
How can i change the format to DD/MM/YY before pasting?
Thanks
Sharan
|
|
|
|
|
How are you copying?
How are you pasting?
Show the relevant code fragments!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have not added any code specific for copy paste.
I have just changed the property of DateTimePicker
is.dateTimePicker.CustomFormat = "dd/MM/yy HH:mm:ss";
is.dateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
is.dateTimePickerExecTime.ShowUpDown = true;
Once right click on Control I select on Copy it copies and I copy it in excel .
From excel I again copy and trying to paste it on control.
|
|
|
|
|
Well what did you expect to happen?
You copy a date in a non-standard form, paste it into a spreadsheet (which identifies it as a date and converts it to a DateTime object), then you copy that datetime object from Excel (in standard-for-that-PC format), and paste it back onto a control that doesn't expect standard format.
There isn't a whole lot that we can do to prevent that causing a problem!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|