943,952 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 445
  • ASP.NET RSS
Aug 5th, 2009
0

Data input via front end form

Expand Post »
hi,

I'm trying to get daya into a new table via form. two fields in new table will pick up data based on look up tables.
pls guide as to how do i proceed . The code is flashing conversion of Data type error.
To avoid the datatype error, i 've already taken category_id and city_id fields in DB as varchar.


Thanks

ASP.NET Syntax (Toggle Plain Text)
  1. protected void btnWinesAvailable_Click(object sender, EventArgs e)
  2. {
  3. conn.Open();
  4. SqlCommand cmd1 = new SqlCommand("Insert into wines_available(name, address, city_id, category_id) values (@name, @address, @cityid, @categoryid)", conn);
  5.  
  6. string a =("select city_id from city_master where city_name= '" + ddlCityname.SelectedItem + "'");
  7. string b= ("select category_id from category_master where category = '"+ ddlCategoryname.SelectedItem+ "'");
  8.  
  9. cmd1.Parameters.Add(new SqlParameter("@name", txtOrgName.Text));
  10. cmd1.Parameters.Add(new SqlParameter("@address", txtAddress.Text));
  11. cmd1.Parameters.Add(new SqlParameter("@cityid", SqlDbType.Char,10)).Value = a;
  12. cmd1.Parameters.Add(new SqlParameter("@categoryid", SqlDbType.Char, 10)).Value = b;
  13.  
  14.  
  15. cmd1.ExecuteNonQuery();
  16. txtOrgName.Text = "";
  17. txtAddress.Text = "";
  18. //txtCityID1.Text = "";
  19. //txtCategoryID1.Text = "";
  20. conn.Close();
  21. conn.Dispose();
  22. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Haimanti is offline Offline
10 posts
since Jul 2009
Aug 5th, 2009
0

Re: Data input via front end form

If what you're trying to do is put the results of the SELECT statements in a and b into the parameters, then it's not going to work like you think it will. Your best bet is to use a stored procedure. That way you'll be able to use variables in the stored proc to hold the results of the two select statements. Then you just plug them into your insert statement in the same stored procedure.
Otherwise, you'll have to make two DB calls from the code before the insert to retrieve the values and then use those values for your insert.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008
Aug 5th, 2009
0

Re: Data input via front end form

Not necessarily. You could do this:
sql Syntax (Toggle Plain Text)
  1. DECLARE @CityId INT, @CategoryId INT
  2. SET @CityId = (SELECT city_id FROM city_master WHERE city_name= 'abc')
  3. SET @CategoryId = (SELECT city_id FROM city_master WHERE city_name='123')
  4.  
  5. INSERT INTO wines_available(name, address, city_id, category_id) VALUES (@name, @address, @cityid, @categoryid)
Last edited by sknake; Aug 5th, 2009 at 2:42 pm.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 6th, 2009
0

Re: Data input via front end form

Thanks for the reply. I was getting error in the i/p parameters data type.

The use of DR and change in parameter worked.

I'll also try with stores precedure n variables as suggested

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. protected void btnWinesAvailable_Click(object sender, EventArgs e)
  3. {
  4. conn.Open();
  5.  
  6. SqlCommand cmd1 = new SqlCommand("Insert into wines_available(name, address, city_id, category_id) values (@name, @address, @cityid, @categoryid)", conn);
  7.  
  8. SqlCommand a = new SqlCommand("select city_id from city_master where city_name= '" + ddlCityname.SelectedItem + "'", conn);
  9. SqlCommand b = new SqlCommand("select category_id from category_master where category = '" + ddlCategoryname.SelectedItem + "'", conn);
  10.  
  11.  
  12. cmd1.Parameters.Add(new SqlParameter("@name", txtOrgName.Text));
  13. cmd1.Parameters.Add(new SqlParameter("@address", txtAddress.Text));
  14.  
  15. SqlDataReader dr1;
  16. dr1 = a.ExecuteReader();
  17. while (dr1.Read())
  18. {
  19. cmd1.Parameters.Add(new SqlParameter("@cityid", SqlDbType.Char, dr1["city_id"].ToString().Trim().Length)).Value = dr1["city_id"].ToString().Trim();
  20. }
  21. dr1.Close();
  22.  
  23. SqlDataReader dr2;
  24. dr2 = b.ExecuteReader();
  25. while (dr2.Read())
  26. {
  27. cmd1.Parameters.Add(new SqlParameter("@categoryid", SqlDbType.Char, dr2["category_id"].ToString().Trim().Length)).Value = dr2["category_id"].ToString().Trim();
  28. }
  29.  
  30. dr2.Close();
  31.  
  32. txtOrgName.Text = "";
  33. txtAddress.Text = "";
  34. //txtCityID1.Text = "";
  35. //txtCategoryID1.Text = "";
  36.  
  37. cmd1.ExecuteNonQuery();
  38. conn.Close();
  39. conn.Dispose();
  40. }

Click to Expand / Collapse  Quote originally posted by bcasp ...
If what you're trying to do is put the results of the SELECT statements in a and b into the parameters, then it's not going to work like you think it will. Your best bet is to use a stored procedure. That way you'll be able to use variables in the stored proc to hold the results of the two select statements. Then you just plug them into your insert statement in the same stored procedure.
Otherwise, you'll have to make two DB calls from the code before the insert to retrieve the values and then use those values for your insert.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Haimanti is offline Offline
10 posts
since Jul 2009
Aug 6th, 2009
0

Re: Data input via front end form

I'm glad you got it working

Please mark this thread as solved if you have found an answer to your question and good luck!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Gridview and Drop Down List
Next Thread in ASP.NET Forum Timeline: Duplication of records





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC