Data input via front end form

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 9
Reputation: Haimanti is an unknown quantity at this point 
Solved Threads: 0
Haimanti Haimanti is offline Offline
Newbie Poster

Data input via front end form

 
0
  #1
Aug 5th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: Data input via front end form

 
0
  #2
Aug 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Data input via front end form

 
0
  #3
Aug 5th, 2009
Not necessarily. You could do this:
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: Haimanti is an unknown quantity at this point 
Solved Threads: 0
Haimanti Haimanti is offline Offline
Newbie Poster

Re: Data input via front end form

 
0
  #4
Aug 6th, 2009
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

  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. }

Originally Posted by bcasp View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Data input via front end form

 
0
  #5
Aug 6th, 2009
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!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC