Click here to Skip to main content
15,916,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to save userid, questionid and the answer user selected in database. I have displayed the questions and the options form the database onto the form. Below is my code till now.i have a table named UserResponses which contains userid,questionid and the answers.
C#
protected void Page_Load(object sender, EventArgs e) {  SqlConnection con = new SqlConnection(connecst);

        //Getting All Questions

        SqlDataAdapter dr = new SqlDataAdapter("select QId, Text from Questions", con);
        DataSet ds = new DataSet();
        dr.Fill(ds, "QId");
        DataList1.DataSource = ds.Tables["QId"];

        dr.Fill(ds, "Text"); 
        DataList1.DataSource = ds.Tables["Text"];

        DataList1.DataBind();


}

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {


            RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
            //Get questionID here
            int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QId"));

            SqlConnection con = new SqlConnection(connecst);
            con.Open();

            sql = "SELECT ID,Answer FROM [Options] inner join Questions on " + "Questions.QId= Options.QID  where Questions.QId=" + QuestionID;
            //Bind the RadiobUttonList here  
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds); DataTable dt = ds.Tables[0];
            RadioButtonList1.DataSource = dt;
            RadioButtonList1.DataTextField = dt.Columns[1].ToString();
            RadioButtonList1.DataValueField = dt.Columns[0].ToString();
            RadioButtonList1.DataBind();

        }
    }


What I have tried:

private void SaveAnswer(int UId, int Qid, string ans)
       {
           foreach (ListItem item in RadioButtonList1.Items)
           {
               if (item.Selected)
               {

                   SqlConnection con = new SqlConnection(connecst);
                   SqlCommand cmd = new SqlCommand();
                   cmd.CommandType = System.Data.CommandType.StoredProcedure;
                   cmd.CommandText = "Insert into UserResponses values(@QuestionID,@Answer)";
                   //simple hiden field no repeater
                   cmd.Parameters.AddWithValue("@UserID", UId);
                   cmd.Parameters.AddWithValue("@QuestionID", Qid);
                   cmd.Parameters.AddWithValue("@Answer", ans);

                   cmd.Connection = con;
                   con.Open();
                   cmd.ExecuteNonQuery();

                   con.Close();

               }

           }
Posted
Updated 19-Aug-19 12:00pm

1 solution

You know how to do queries safe to SQL injection:
C#
cmd.CommandText = "Insert into UserResponses values(@QuestionID,@Answer)";

but you are still using queries subject to SQL injection:
C#
sql = "SELECT ID,Answer FROM [Options] inner join Questions on " + "Questions.QId= Options.QID  where Questions.QId=" + QuestionID;

Why ?

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Richard Deeming 22-Aug-19 8:51am    
Well, technically that query isn't vulnerable, because the value being concatenated is an int. If the user tried to inject SQL into the value, the code would fall over on the Convert.ToInt32 line above.

But it's still a bad habit that needs to be discouraged. And it would be far too easy to change the data type used and introduce a SQLi vulnerability without realising it.
Patrice T 22-Aug-19 9:29am    
I agree with you, My solution is to discourage OP to do this because sooner or later, he will be caught with an injection.

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