Click here to Skip to main content
15,881,740 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to make Data Recovery Software using C# Pin
Gerry Schmitz20-Feb-18 10:45
mveGerry Schmitz20-Feb-18 10:45 
AnswerRe: How to make Data Recovery Software using C# Pin
Dave Kreskowiak20-Feb-18 13:50
mveDave Kreskowiak20-Feb-18 13:50 
AnswerRe: How to make Data Recovery Software using C# Pin
MadMyche22-Feb-18 4:44
professionalMadMyche22-Feb-18 4:44 
QuestionDateTimePicker Override right click events Pin
manju 319-Feb-18 23:36
manju 319-Feb-18 23:36 
AnswerRe: DateTimePicker Override right click events Pin
Ralf Meier20-Feb-18 0:34
mveRalf Meier20-Feb-18 0:34 
QuestionSQL Connection and insertion of data in C# form application Pin
Member 1368376518-Feb-18 1:09
Member 1368376518-Feb-18 1:09 
AnswerRe: SQL Connection and insertion of data in C# form application Pin
Richard MacCutchan18-Feb-18 1:39
mveRichard MacCutchan18-Feb-18 1:39 
AnswerRe: SQL Connection and insertion of data in C# form application Pin
Eddy Vluggen18-Feb-18 3:02
professionalEddy Vluggen18-Feb-18 3:02 
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.

C#
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())
        {
            // declare success here!
        }
        else
        {
            // panic here.
        }
    }
}
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 Smile | :)
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]

SuggestionRe: SQL Connection and insertion of data in C# form application Pin
Richard Deeming19-Feb-18 8:18
mveRichard Deeming19-Feb-18 8:18 
QuestionTesting methodologies Pin
Hila Berger18-Feb-18 0:54
Hila Berger18-Feb-18 0:54 
AnswerRe: Testing methodologies Pin
Richard MacCutchan18-Feb-18 1:35
mveRichard MacCutchan18-Feb-18 1:35 
AnswerRe: Testing methodologies Pin
Eddy Vluggen18-Feb-18 2:41
professionalEddy Vluggen18-Feb-18 2:41 
GeneralRe: Testing methodologies Pin
BillWoodruff18-Feb-18 9:53
professionalBillWoodruff18-Feb-18 9:53 
GeneralRe: Testing methodologies Pin
Eddy Vluggen18-Feb-18 10:36
professionalEddy Vluggen18-Feb-18 10:36 
AnswerRe: Testing methodologies Pin
BillWoodruff18-Feb-18 9:50
professionalBillWoodruff18-Feb-18 9:50 
QuestionSale Invoice Pin
Member 1368352817-Feb-18 20:37
Member 1368352817-Feb-18 20:37 
AnswerRe: Sale Invoice Pin
Richard MacCutchan17-Feb-18 21:06
mveRichard MacCutchan17-Feb-18 21:06 
AnswerRe: Sale Invoice Pin
OriginalGriff17-Feb-18 22:42
mveOriginalGriff17-Feb-18 22:42 
QuestionTop Level Properties Pin
User9874317-Feb-18 16:26
professionalUser9874317-Feb-18 16:26 
AnswerRe: Top Level Properties Pin
Mycroft Holmes17-Feb-18 22:34
professionalMycroft Holmes17-Feb-18 22:34 
AnswerRe: Top Level Properties Pin
BillWoodruff17-Feb-18 23:36
professionalBillWoodruff17-Feb-18 23:36 
GeneralRe: Top Level Properties Pin
User9874318-Feb-18 6:56
professionalUser9874318-Feb-18 6:56 
GeneralRe: Top Level Properties Pin
BillWoodruff18-Feb-18 8:22
professionalBillWoodruff18-Feb-18 8:22 
GeneralRe: Top Level Properties Pin
User9874318-Feb-18 18:12
professionalUser9874318-Feb-18 18:12 
GeneralRe: Top Level Properties Pin
Mycroft Holmes18-Feb-18 13:14
professionalMycroft Holmes18-Feb-18 13:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.