Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem in gridview.

In gridview I have two template fields.
One has a label showing questions stored in a database, the other has a textbox.

I want to store the text entered in the textbox, but there is a problem.
When I tried to store the question and the text into another table, it stored only the question, not the entered text.

How can I get the third table to accept both the question and text?
Posted
Updated 14-May-10 5:33am
v3
Comments
Sandeep Mewara 14-May-10 9:16am    
You are doing something somewhere wrong! You have to post more... show some code where you are trying to get the grid values!
Ankur\m/ 14-May-10 9:19am    
How do you expect someone to solve an issue in your code without actually seeing it.
Add the relevant code snippet to your question.
radix3 14-May-10 10:14am    
Please enter the code we are programmers not psychic
Dalek Dave 14-May-10 11:33am    
Edited for clarity and ease of reading.

1 solution

hello,
You can save your data in a seperate table via GridView's RowCommand Event.

Database structure :
--------------------
Table Questions : qid (PK), question
Table Answers : aid (PK), qid (FK), Answer

You have 3 columns in your GridView
1. Question Column i.e. TemplateField containing a Label
2. Answer Column i.e. TemplateField containing a TextBox
3. The third column is also a TemplateField containing a Button
The button's CommandArgumen is set to the Question Id (qid) in order
save the data with QuestionId in a referencing Answer Table

here is the code for PageLoad event (bind you r grid) :

if (!IsPostBack)
      {
          string Query = "SELECT * FROM Questions";

          SqlCommand cmd = new SqlCommand(Query, CreateConnection());
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();

          da.Fill(dt);


          GridView1.DataSource = dt;
          GridView1.DataBind();
      }


here is the code for you saving of data i.e. RowCommand event of the GridView:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Get the question id (qid)
        string arg = e.CommandArgument.ToString();

        //Get the row thas has received the user action
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        TextBox MyAnswer = (TextBox)row.FindControl("txtAnswer");

        //create query
        string SaveAnswerQuery = "INSERT INTO Answers(qid,Answer) VALUES(" + arg + ",'" + MyAnswer.Text + "')";

        //save via ADO.NET
        //your own code here
    }
 
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