11 Replies - 13026 Views - Last Post: 25 September 2012 - 12:00 AM Rate Topic: -----

#1 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Inserting values from a text box to a database

Posted 18 September 2012 - 04:37 AM

I have a textbox that takes values on clicking of the submit button. I want to take these values and store them in a separate database. Can you please help me in this regard?

Thank you
Is This A Good Question/Topic? 0
  • +

Replies To: Inserting values from a text box to a database

#2 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Re: Inserting values from a text box to a database

Posted 19 September 2012 - 02:34 AM

Hi,

I wonder if you have an idea why you do not get help here so far...

I hope you understand that it is impossible for us to help you because amount of information is so small! Please provide more info like what you have done so far, what problems occured, do you have any database where you want to store your info from textbox and so on. Because if you dodn't do anything yet i suggest you start from tutorials first.

Kind regards.
Was This Post Helpful? 1
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Inserting values from a text box to a database

Posted 19 September 2012 - 07:44 AM

The OP most likely hasn't done any work on this. He/she is most likely just looking for someone to do the work for him/her. Don't waste your "breath" on these...

This post has been edited by h4nnib4l: 19 September 2012 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Inserting values from a text box to a database

Posted 19 September 2012 - 10:11 PM

View Postc#think#, on 19 September 2012 - 03:04 PM, said:

Hi,

I wonder if you have an idea why you do not get help here so far...

I hope you understand that it is impossible for us to help you because amount of information is so small! Please provide more info like what you have done so far, what problems occured, do you have any database where you want to store your info from textbox and so on. Because if you dodn't do anything yet i suggest you start from tutorials first.

Kind regards.


I am sorry for I am just a beginner in ASP.NET and the project I am working on now needs to be completed in a week so I am pretty tensed about it.About what I have done so far...here is the code I managed to write.

protected void SubmitButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Server=myservername;Database=NewEntries.mdf;Trusted_Connection=True");
            con.Open();         
                     
            string qry = "insert into NewTechMag values( '" + TextBox1 + "')";
            SqlCommand cmd = new SqlCommand(qry, con);
           
            cmd.ExecuteNonQuery();
            con.Close();
        }



But whatever data I give into the textbox is not getting appended in the tables of the NewEntries Database.Can you please help in this regard.
Thank you.

View Posth4nnib4l, on 19 September 2012 - 08:14 PM, said:

The OP most likely hasn't done any work on this. He/she is most likely just looking for someone to do the work for him/her. Don't waste your "breath" on these...


Please don't be so harsh on me.I am sorry for I am just a beginner in ASP.NET and the project I am working on now needs to be completed in a week so I am pretty tensed about it.About what I have done so far...here is the code I managed to write.

protected void SubmitButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Server=myservername;Database=NewEntries.mdf;Trusted_Connection=True");
            con.Open();         
                     
            string qry = "insert into NewTechMag values( '" + TextBox1 + "')";
            SqlCommand cmd = new SqlCommand(qry, con);
           
            cmd.ExecuteNonQuery();
            con.Close();
        }



But whatever data I give into the textbox is not getting appended in the tables of the NewEntries Database.Can you please help in this regard.
Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 396
  • Joined: 04-January 11

Re: Inserting values from a text box to a database

Posted 19 September 2012 - 10:49 PM

Why don't you try creating a stored procedure in SQL that accepts a string as a parameter.

In your C# code, call this stored procedure and pass the textbox value. Try to debug your code to see if the text of your textbox is being passed properly.
Was This Post Helpful? 0
  • +
  • -

#6 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Re: Inserting values from a text box to a database

Posted 20 September 2012 - 12:04 AM

Hi!

You see, little effort from your side and you already have some replies...

Regarding your problem...
Is this SqlConnection string is real or you just do not want to provide your real connection to us?
I mean that if it is real then it is incorrect. Search the web for correct Connection string. These are different, depending on a provider(MS Access, MSSQL, Oracle, MySql and so on).

How many and what type of columns do you have in your newTechMag table?

What is TextBox1? Is it a string variable where you store value from some TextBox or is it a TextBox control that you have on your form/page?
If it is a control then you have to pass a 'Text' property of that control to SQL query: TextBox1.Text. Not just TextBox1.

Kind regards...
Was This Post Helpful? 1
  • +
  • -

#7 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Re: Inserting values from a text box to a database

Posted 20 September 2012 - 12:15 AM

View Posth4nnib4l, on 19 September 2012 - 07:44 AM, said:

Don't waste your "breath" on these...


Hey :)

Mostly i am agree with you, i just thought that guy didn't know how and what to ask :P. I remember myself first time on programming forums... :stupid:

But anyway, you are right about these kind of posts, thanks!
Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Inserting values from a text box to a database

Posted 20 September 2012 - 01:14 AM

View Postc#think#, on 20 September 2012 - 12:34 PM, said:

Hi!

You see, little effort from your side and you already have some replies...

Regarding your problem...
Is this SqlConnection string is real or you just do not want to provide your real connection to us?
I mean that if it is real then it is incorrect. Search the web for correct Connection string. These are different, depending on a provider(MS Access, MSSQL, Oracle, MySql and so on).

How many and what type of columns do you have in your newTechMag table?

What is TextBox1? Is it a string variable where you store value from some TextBox or is it a TextBox control that you have on your form/page?
If it is a control then you have to pass a 'Text' property of that control to SQL query: TextBox1.Text. Not just TextBox1.

Kind regards...



Thank you very much for your response in this regard.After I posted my reply, I googled several webpages and got a lot of information and then I came up with some changes in the code.Here it is.

 protected void SubmitButton_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\ccbatk\Documents\Visual Studio 2010\Projects\TechMagSurvey\TechMagSurvey\App_Data\NewEntries.mdf";Integrated Security=True;User Instance=True";
                     
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert into NewTechMag(Tech Magazines) VALUES ('" + TextBox1.Text + "')";
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }



I would like to mention that there is a table called NewTechMag with the column 'Tech Magazines'.The textbox1 is the textbox which is used to take values and on clicking of submit button, is supposed to make the values go into the database called NewEntries.

I would certainly like to thank you for taking your time to help a beginner like me.

View PostNovember-06, on 20 September 2012 - 11:19 AM, said:

Why don't you try creating a stored procedure in SQL that accepts a string as a parameter.

In your C# code, call this stored procedure and pass the textbox value. Try to debug your code to see if the text of your textbox is being passed properly.


Thank you for your reply but since I am a beginner, I have no idea as to how to create a stored procedure.
Was This Post Helpful? 0
  • +
  • -

#9 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,488
  • Joined: 07-April 08

Re: Inserting values from a text box to a database

Posted 20 September 2012 - 09:01 AM

First, if you can, change the name of your column to remove the spaces. Putting spaces in columns is horrible design.

Second, if you can't change it then you need to enclose it in square brackets ([]) otherwise when SqlServer tries to execute the insert it will fail because the insert statement isn't well formed.

Third, my suggestion to you is use the following connection string (You can get any and all connection strings from connectionstrings.com)
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;



Last, Use sql parameters and not inline sql like you currently are. You are opening yourself up for sql injection when you inline like that. Look at the SqlCommand.Parameters msdn page for more information

This post has been edited by rgfirefly24: 20 September 2012 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

#10 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Inserting values from a text box to a database

Posted 20 September 2012 - 08:40 PM

View Postrgfirefly24, on 20 September 2012 - 09:31 PM, said:

First, if you can, change the name of your column to remove the spaces. Putting spaces in columns is horrible design.

Second, if you can't change it then you need to enclose it in square brackets ([]) otherwise when SqlServer tries to execute the insert it will fail because the insert statement isn't well formed.

Third, my suggestion to you is use the following connection string (You can get any and all connection strings from connectionstrings.com)
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;



Last, Use sql parameters and not inline sql like you currently are. You are opening yourself up for sql injection when you inline like that. Look at the SqlCommand.Parameters msdn page for more information


Thank you very much for your reply.Unfortunately, I need to give the column name as it is, so I will use your suggestion of implementing square brackets.

As for the connection string, I shall use it and see if it works.

Lastly, I am still a beginner in ASP.NET and so I don't know about sql injection...moreover I don't think anyone would want to do an SQL injection attack for a bunch of technical magazines.

I shall get back to you after implementing your above suggestions.Thank you.
Was This Post Helpful? 0
  • +
  • -

#11 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Inserting values from a text box to a database

Posted 24 September 2012 - 06:28 AM

Quote

I am still a beginner in ASP.NET and so I don't know about sql injection...moreover I don't think anyone would want to do an SQL injection attack for a bunch of technical magazines.


What better time to learn good programming practices than when you're a beginner and haven't really developed bad habits yet? Check this tutorial out for help with parameterizing your queries. And you may not see why anyone would want to perform SQL injection against a bunch of tech magazines, but that doesn't mean that nobody will. Basic security measures shouldn't be considered optional, based on the assumed attractiveness of the target.
Was This Post Helpful? 1
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Inserting values from a text box to a database

Posted 25 September 2012 - 12:00 AM

View Posth4nnib4l, on 24 September 2012 - 06:58 PM, said:

Quote

I am still a beginner in ASP.NET and so I don't know about sql injection...moreover I don't think anyone would want to do an SQL injection attack for a bunch of technical magazines.


What better time to learn good programming practices than when you're a beginner and haven't really developed bad habits yet? Check this tutorial out for help with parameterizing your queries. And you may not see why anyone would want to perform SQL injection against a bunch of tech magazines, but that doesn't mean that nobody will. Basic security measures shouldn't be considered optional, based on the assumed attractiveness of the target.


Thank you very much for that link. That certainly gave me a good insight to SQL injection and the dangers of not using parameters in the queries. I shall try and implement the same :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1