|
Hi,
I am new on here and would like some help with dealing with what should be a simple task. I have a small form which is simply a data entry form for a single field (the second being a database autonumber). After running the SQL script I want either a messagebox to show that an entry already exists in the database or that the insert has been successful.
Below is my C# code.
Please can someone help as to how to achieve what I need! The above code obviously gives a successful message regardless of the real result.
Many thanks,
private void SaveButton_Click(object sender, EventArgs e)
{
if (contact_typeTextBox.Text == "" && contact_typeTextBox.Text == "")
{
errorProvider1.SetError(contact_typeTextBox, "Field must have valid text.");
}
else
{
errorProvider1.SetError(contact_typeTextBox, "");
da.InsertCommand = new SqlCommand("IF NOT EXISTS(SELECT * FROM Contact_Types WHERE contact_type=@contacttype) INSERT INTO Contact_Types (contact_type) VALUES(@contacttype)", cs);
da.InsertCommand.Parameters.Add("@contacttype", SqlDbType.NVarChar).Value = contact_typeTextBox.Text;
cs.Open();
MessageBox.Show("Record Successfully Saved");
cs.Close();
foreach (var conrol in Controls)
{
var textbox = conrol as TextBox;
if (textbox != null)
textbox.Clear();
}
}
}
|
|
|
|
|
What are da and cs in your code? All I see there is a message being shown that you successfully opened some form of connection but without anymore context, I can't offer more concrete advice. I could assume that da is a DataAdapter based off your naming but I could well be wrong. To be honest, using anything other than a straightforward SqlCommand here seems overkill. If I were you, I'd simplify this to:
SqlCommand command = new SqlCommand("IF NOT EXISTS(SELECT * FROM Contact_Types WHERE contact_type=@contacttype) INSERT INTO Contact_Types (contact_type) VALUES(@contacttype)");
command.Parameters.Add("@contacttype", SqlDbType.NVarChar).Value = contact_typeTextBox.Text;
cs.Open();
try
{
command.ExecuteNonQuery();
}
finally
{
cs.Close();
}
This space for rent
|
|
|
|
|
Your assumptions are correct - da = DataAdapter and cs = SqlConnection.
I am new to C# and my code has been trial and error, so is far from perfect. Thanks for your help. I will try it in my code.
|
|
|
|
|
Member 13218552 wrote: my code has been trial and error
Don't. Stop, think, research. Most of the documentation includes example code which gives you a starter - and taking the time to think about what you are trying to do instead of "try it and see if it works" can save you huge amounts of time. And grief.
Look at this code: you have been trying to fix this for over half a day that we know of. 30 minutes spent in thinking and research could have given you the solution hours ago...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How about we assign an Int32 to that NonQuery() to get the Rows Affected; if 0 then record exists and if 1 then it was inserted
Int32 RowsAffected = -1;
string MessageContent;
try {
RowsAffected = command.ExecuteNonQuery();
}
catch (Exception ex) {
MessageContent = ex.Message;
// Handle the error
finally {
// clean up
}
if (RowsAffected == 0) { MessageContent = "Record already exists"; }
else if (RowsAffected == 1) { MessageContent = "Record inserted"; }
MessageBox.Show(MessageContent);
Director of Transmogrification Services
Shinobi of Query Language
Master of Yoda Conditional
|
|
|
|
|
Why are you answering me? The OP won't get notified that there was an answer if it goes to someone else.
This space for rent
|
|
|
|
|
I replied as a collaboration. I found your code to be good and similar to what I would do, and thought I could build onto it.
Director of Transmogrification Services
Shinobi of Query Language
Master of Yoda Conditional
|
|
|
|
|
By all means refer to it, but refer to it on the OP's post. I'm not looking for credit, so I don't mind.
This space for rent
|
|
|
|
|
Member 13218552 wrote: gives a successful message regardless of the real result. That is because you are not checking any result to see if it succeeded. In fact you do not even perform any database function before displaying the message. Assuming that da is a DataAdapter, you should be using the SqlDataAdapter.Update Method (System.Data.SqlClient)[^] to perform your changes and, most importantly, checking the result when the method returns.
|
|
|
|
|
dtjr.DefaultView.RowFilter = " Journal_Name Like '" + cbo_jrnl.Text.Trim() + "*'";
is working fine,
But i want to filter all jounral_name stats with number from 0-9
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
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 from. And we have no idea what your "jounral_name stats" are, or what they look like - I assume that's a typo, and it should be "Journal_Name stats" but that doesn't help when you have no idea what it contains or what exactly you are trying to do with it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 23-May-17 4:37am.
|
|
|
|
|
You can chain multiple items together in a filter using AND and OR , so one possibility would be to do Journal_Name LIKE '0*' OR Journal_Name LIKE '1*' ... and so on. I'm not saying it's the best option but it is AN option.
This space for rent
|
|
|
|
|
you could try LIKE '[0-9]*'
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
The OP could - personally speaking, I don't use DataTable's
This space for rent
|
|
|
|
|
how to get double click event to be fired on gridview cell in c# as there is no cell doubleclik event
available as in vb.net .
Hence how can i achieve cell_doubleclick event on gridview in c#.
modified 23-May-17 1:01am.
|
|
|
|
|
|
thanks for the reply but data gridview is in vb.net not in c# so i cant use
|
|
|
|
|
No, it's a .NET control, so it can be used by any .NET language.
|
|
|
|
|
m talking about asp.net but u said abt vb.net ..datagrid view control is in vb.net where as asp.net has
gridview as gridview control.
|
|
|
|
|
Mayank Kumar wrote: m talking about asp.net You posted your question in the C# forum and made no mention of ASP.NET, your original question being:
Quote: how to get double click event to be fired on gridview cell in c# so how are we to guess that you are talking about ASP.NET?
I suggest you post a properly detailed question in the ASP.NET forum, with details of what controls you are using and where your problem is.
|
|
|
|
|
apologies, can you answer me the same
|
|
|
|
|
There is no server side event handler for the ASP.NET GridView. You will have to write your own JavaScript code to handle this. You could use this article[^] as a starting point.
This space for rent
|
|
|
|
|
Hi,
I have two JSON objects:
JSON Object 1
[{
"date":
[
"date-value1",
"date-value2,
........ ,
"date-value729"
]
}]
JSON Object 2
{
"data": [
{
"body": [ "body1" ],
"link": "link1",
"title": [ "title1" ]
},
...........
{
"body": [ "body729" ],
"link": "link729",
"title": [ "title729" ]
}
]
}
How can I merge the properties of these 2 JSON objects and have the following example as an output for all 729 records of data:
{
"data": [
{
"body": [ "body1" ],
"link": "link1",
"title": [ "title1" ].
"date": "date1"
},
...........
{
"body": [ "body729" ],
"link": "link729",
"title": [ "title729" ]
"date": "date729"
}
]
}
Examples that I found online are mainly about merging two identical JSON objects.
Thank you in advance for your time and consideration.
|
|
|
|
|
You would need to do it manually. Create a class for each type, and one that contains all the properties. Read the two sets into the first two classes, merge the properties into the third, and create a new JSON stream from that.
|
|
|
|
|
Thanks, it worked as you guided. I thought maybe there is already some sort of online tools to import and export such task.
|
|
|
|