Reputation: 1295
I am using .net and I am changing my .aspx.cs file to add the user choice in radio button to a row in database.
private DataSet CreateData()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("OptionID", System.Type.GetType("System.String")));
table.Columns.Add(new DataColumn("Option", System.Type.GetType("System.String")));
DataRow row1 = table.NewRow();
row1["OptionID"] = "1";
row1["Option"] = "option 1";
DataRow row2 = table.NewRow();
row2["OptionID"] = "2";
row2["Option"] = "two";
DataRow row3 = table.NewRow();
row3["OptionID"] = "3";
row3["Option"] = "three";
table.Rows.Add(row1);
table.Rows.Add(row2);
table.Rows.Add(row3);
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds;
}
private void bindRadioList()
{
DataSet ds = CreateData();
RadioButtonList1.DataSource = ds;
RadioButtonList1.DataTextField = "Option";
RadioButtonList1.DataValueField = "OptionID";
RadioButtonList1.DataBind();
if (RadioButtonList1.Items.Count > 0)
RadioButtonList1.Items[0].Selected = true; //you can set a selected items you want.
}
public void Button1_Click(object sender, System.EventArgs e)
{
Session["choice"] = RadioButtonList1.SelectedValue;
}
But I do not know how could I get this value at my last step when people click submit at the next page. I tried:
string sqlIns = "INSERT INTO Choice (Email, Choice) VALUES (@Email, @Choice)";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
conn.Open();
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, conn);
cmdIns.Parameters.Add("@Email", userRecord.email);
cmdIns.Parameters.Add("@Choice", (string)Session["choice"]);
cmdIns.ExecuteNonQuery();
But I got this error:
Exception Details: System.Data.SqlClient.SqlException: The parameterized query '(@Email nvarchar(19),@Choice nvarchar(4000))INSERT INTO Choice (' expects the parameter '@Choice', which was not supplied.
Upvotes: 1
Views: 460
Reputation: 1295
I found out the problem and solution for this: The problem is Session["choice"] = Null and it is not acceptable under MSN standard. So I need to add this to prevent Null value :
if ((string)Session["choice"] == null)
{
cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = DBNull.Value;
}
else
{
// cmdIns.Parameters.AddWithValue("@Choice", (string)Session["choice"]);
cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = (string)Session["choice"];
}
To fix the null value, in .aspx file(I still can't figure out why the old code give null, but this one can avoid it)
<div>
<asp:RadioButton ID="RadioButton1" GroupName="Group1" Text="Choice 1" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" />
<asp:RadioButton ID="RadioButton2" GroupName="Group1" Text="Choice 2" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" />
<asp:RadioButton ID="RadioButton3" GroupName="Group1" Text="Choice 3" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" />
</div>
and .cs file:
protected void Button1_Click(object sender, System.EventArgs e)
{
// Session["choice"] = RadioButtonList1.SelectedItem.Text;
if (RadioButton1.Checked)
{
Session["choice"] = "Choice 1";
}
if (RadioButton2.Checked)
{
Session["choice"] = "Choice 2";
}
if (RadioButton3.Checked)
{
Session["choice"] = "Choice 3";
}
}
Upvotes: 0
Reputation: 641
please check your store procedure where you are passing paramerters there you are not passing parameters @Choice
Upvotes: 0
Reputation: 98750
Use AddWithValue
instead of Add
like;
cmdIns.Parameters.AddWithValue("@Email", userRecord.email);
cmdIns.Parameters.AddWithValue("@Choice", (string)Session["choice"]);
From it's documentation;
SqlParameterCollection.Add Method (String, Object)
overload has been deprecated. UseAddWithValue(String parameterName, Object value)
instead. http://go.microsoft.com/fwlink/?linkid=14202"
Or you can use (String, SqlDbType)
overload (which I almost always prefer instead of AddWithValue
) of Add
method like (I assume both your column is NVarChar
type);
cmdIns.Parameters.Add("@Email", SqlDbType.NVarChar).Value = userRecord.email;
cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = (string)Session["choice"];
Also use using
statement to dispose your SqlConnection
adn SqlCommand
like;
using(SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]))
using(SqlCommand cmdIns = conn.CreateCommand())
{
cmdIns.CommandText = sqlIns;
...
...
}
Upvotes: 4