different ValueField and TextField for a dropdownlist
-
i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte
<asp:DropDownList ID="ddlCType" runat="server"
DataSourceID="SqlDataSource1" DataTextField="CouponName"
DataValueField="CouponTypeID"style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
</asp:DropDownList>
try { ErrorMessage.Text = ""; cnx.Open(); SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx); insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt); insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery(); ErrorMessage.Text = "Records inserted successfully"; cnx.Close(); }
-
i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte
<asp:DropDownList ID="ddlCType" runat="server"
DataSourceID="SqlDataSource1" DataTextField="CouponName"
DataValueField="CouponTypeID"style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
</asp:DropDownList>
try { ErrorMessage.Text = ""; cnx.Open(); SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx); insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt); insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery(); ErrorMessage.Text = "Records inserted successfully"; cnx.Close(); }
haleemasher wrote:
insertCmd.Parameters["@Id"].Value = ddlCType.Text;
You should use ddlCType.SelectedValue property for dropdownlist's selected item's value. :laugh:
Be an Eagle, Sky is Yours.
-
haleemasher wrote:
insertCmd.Parameters["@Id"].Value = ddlCType.Text;
You should use ddlCType.SelectedValue property for dropdownlist's selected item's value. :laugh:
Be an Eagle, Sky is Yours.
Same error may i change this code also
insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
-
i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte
<asp:DropDownList ID="ddlCType" runat="server"
DataSourceID="SqlDataSource1" DataTextField="CouponName"
DataValueField="CouponTypeID"style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
</asp:DropDownList>
try { ErrorMessage.Text = ""; cnx.Open(); SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx); insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt); insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery(); ErrorMessage.Text = "Records inserted successfully"; cnx.Close(); }
-
i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte
<asp:DropDownList ID="ddlCType" runat="server"
DataSourceID="SqlDataSource1" DataTextField="CouponName"
DataValueField="CouponTypeID"style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
</asp:DropDownList>
try { ErrorMessage.Text = ""; cnx.Open(); SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx); insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt); insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery(); ErrorMessage.Text = "Records inserted successfully"; cnx.Close(); }
haleemasher wrote:
insertCmd.Parameters["@Id"].Value = ddlCType.Text
Use insertCmd.Parameters["@Id"].Value = Convert.ToByte(ddlCType.SelectedValue); Viral
Viral My Site Tips & Tracks
-
haleemasher wrote:
insertCmd.Parameters["@Id"].Value = ddlCType.Text
Use insertCmd.Parameters["@Id"].Value = Convert.ToByte(ddlCType.SelectedValue); Viral
Viral My Site Tips & Tracks
it show another error Input sting was not in correct format
-
it show another error Input sting was not in correct format
sorry my mistake Use insertCmd.Parameters["@Id"].Value = Convert.Int32(ddlCType.SelectedValue); here you can use Int32 or Int16 depends on your requirement. Hope this help Viral
Viral My Site Tips & Tracks