Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to enter some details to my database using a webform and it did not get the values database.
When I tried to insert data it did not show me error, it will re-direct to the InsertSuccess.aspx page.
but when I check the entered data in database, no more data.
Please show me the path to correct it.
Thanks in advance


Here is my code

protected void Button1_Click(object sender, EventArgs e)
   {

       string a = TextBox1.Text;
       string b = TextBox2.Text;
       string c = TextBox3.Text;
       string d = TextBox4.Text;
       int ef = Convert.ToInt32(TextBox5.Text);
       int f = Convert.ToInt32(TextBox6.Text);
       int mo = Convert.ToInt32(TextBox12.Text);
       string g = TextBox7.Text;
       string h = TextBox11.Text;
       string i = DropDownList6.SelectedItem.ToString();
       string j = DropDownList7.SelectedItem.ToString();
       string k = DropDownList3.SelectedItem.ToString();
       string l = DropDownList4.SelectedItem.ToString();
       string m = DropDownList5.SelectedItem.ToString();
       string n = TextBox8.Text;
       string o = TextBox9.Text;
       OleDbConnection conn = new OleDbConnection();
       OleDbCommand comm = new OleDbCommand();
       conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=K:\Morn\db1.mdb";
       try
       {
           conn.Open();
           string myQuery = "INSERT INTO Lawyer values ('" + a + "','" + b + "','" + c + "','" + d + "'," + ef + "," + f + "," + mo + ",'" + g + "','" + h + "','" + i + "','" + j + "','" + k + "','" + l + "','" + m + "','" + n + "','" + o + "')";
           comm = new OleDbCommand(myQuery, conn);
           comm.ExecuteNonQuery();

           Response.Redirect("InsertSuccess.aspx");


       }
       catch (Exception ex)
       {
           //Response.Redirect("DatabaseError.aspx");
           Response.Write(ex.Message.ToString());

       }
       finally
       {
           conn.Close();
       }
Posted
Updated 2-Oct-10 18:03pm
v2
Comments
HimanshuJoshi 3-Oct-10 0:03am    
Edited for code block and some minor grammatical mistakes
virang_21 3-Oct-10 3:39am    
You should use proper variable names rather than a,b,c etc. otherwise you / someone else will have a hard time managing this code later !

Step 1:
Using DEBUGGER, check what 'myQuery' is formed just before executing it.

Step 2:
Try this query formed directly into your DB. See, if this formed correctly and works.

Step 3:
Now, why have you created 'comm' object two times? Once done, you can just use its properties to define query and connection string. Something like, comm.ConnectionString = "......";

Step 4:
Set a integer return value for 'comm.ExecuteNonQuery();'. ExecuteNonQuery woud return the number of rows affected. If you get that more than zero then do a redirect.

Hope all these steps above should tell you the issue. Try.
 
Share this answer
 
hey debug your page and check result of your variable "string myquery".

you can run result directly in query browser and see to it are you getting ne error.

there might be problem in building query.please see to it . :-)
 
Share this answer
 
hi ther
use parameters instead like this
 string myQuery = "INSERT INTO Laywers(fields go here separated by commas) VALUES(?,?,?)"; // Add a question mark according to how many fields u have 6 fields then put 6 question mark
comm.CommandQuery = myQuery;
comm.Parameters.AddWithValue("?", Textbox1.Text); // link each parameter to corresponding field
comm.Parameter.AddWithValue("?", Textbox2.Text); // repeat  step for each parameter
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900